PC Review


Reply
Thread Tools Rate Thread

Before Save Event is not working when called from another Procedure

 
 
TW Bake
Guest
Posts: n/a
 
      26th Jan 2007
Hi All,

I've taken several stabs at this and am not getting anywhere. When I
use the save button on the toolbar, the BeforeSave event below works as
expected. However, when I envoke a save from another macro (btnSave),
the event does not actually save when the filename is not the
recommended name.

When a macro button is pressed, this event should check if the file
name matches the recommended name (ie Draft1.xls), if not (ie
Draft2.xls) the user shoule be prompted with the SaveAs Dialog box.
The user may or may not rename the file. When the user presses SAVE on
the dialog box, the file should save ... but it does not.

Any help would be appreciated.

thanks,

TW Baker


--------------------------------------------------------
Sub btnSave()
ThisWorkbook.Save
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
tmpString = "Draft1.xls" '****RECOMMENDED NAME
If ThisWorkbook.Name <> tmpString Then
vfile = Application.GetSaveAsFilename(tmpString)
If vfile <> False Then
Application.EnableEvents = False
ThisWorkbook.SaveAs vfile '***This does not happen when
save is called from another macro.
Application.EnableEvents = True
Cancel = True
Else
Cancel = True
End If
End If
End Sub

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SkxHV2hpeg==?=
Guest
Posts: n/a
 
      27th Jan 2007
Please post the code that fails.

"TW Bake" wrote:

> Hi All,
>
> I've taken several stabs at this and am not getting anywhere. When I
> use the save button on the toolbar, the BeforeSave event below works as
> expected. However, when I envoke a save from another macro (btnSave),
> the event does not actually save when the filename is not the
> recommended name.
>
> When a macro button is pressed, this event should check if the file
> name matches the recommended name (ie Draft1.xls), if not (ie
> Draft2.xls) the user shoule be prompted with the SaveAs Dialog box.
> The user may or may not rename the file. When the user presses SAVE on
> the dialog box, the file should save ... but it does not.
>
> Any help would be appreciated.
>
> thanks,
>
> TW Baker
>
>
> --------------------------------------------------------
> Sub btnSave()
> ThisWorkbook.Save
> End Sub
>
> Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
> Boolean)
> tmpString = "Draft1.xls" '****RECOMMENDED NAME
> If ThisWorkbook.Name <> tmpString Then
> vfile = Application.GetSaveAsFilename(tmpString)
> If vfile <> False Then
> Application.EnableEvents = False
> ThisWorkbook.SaveAs vfile '***This does not happen when
> save is called from another macro.
> Application.EnableEvents = True
> Cancel = True
> Else
> Cancel = True
> End If
> End If
> End Sub
>
>

 
Reply With Quote
 
TW Bake
Guest
Posts: n/a
 
      29th Jan 2007
I did, it's under my description of the problem .... help!!!

On Jan 26, 7:18 pm, JLGWhiz <JLGW...@discussions.microsoft.com> wrote:
> Please post the code that fails.
>
>
>
> "TW Bake" wrote:
> > Hi All,

>
> > I've taken several stabs at this and am not getting anywhere. When I
> > use the save button on the toolbar, the BeforeSave event below works as
> > expected. However, when I envoke a save from another macro (btnSave),
> > the event does not actually save when the filename is not the
> > recommended name.

>
> > When a macro button is pressed, this event should check if the file
> > name matches the recommended name (ie Draft1.xls), if not (ie
> > Draft2.xls) the user shoule be prompted with the SaveAs Dialog box.
> > The user may or may not rename the file. When the user presses SAVE on
> > the dialog box, the file should save ... but it does not.

>
> > Any help would be appreciated.

>
> > thanks,

>
> > TW Baker

>
> > --------------------------------------------------------
> > Sub btnSave()
> > ThisWorkbook.Save
> > End Sub

>
> > Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
> > Boolean)
> > tmpString = "Draft1.xls" '****RECOMMENDED NAME
> > If ThisWorkbook.Name <> tmpString Then
> > vfile = Application.GetSaveAsFilename(tmpString)
> > If vfile <> False Then
> > Application.EnableEvents = False
> > ThisWorkbook.SaveAs vfile '***This does not happen when
> > save is called from another macro.
> > Application.EnableEvents = True
> > Cancel = True
> > Else
> > Cancel = True
> > End If
> > End If
> > End Sub- Hide quoted text -- Show quoted text -


 
Reply With Quote
 
kounoike
Guest
Posts: n/a
 
      30th Jan 2007
i have no idea about why the code "ThisWorkbook.SaveAs vfile" in
Workbook_BeforeSave does not work when evoked from a macro. this is one
example to work around, though i'm not sure if this is a right way and works
in every cases.

in standard module

Sub btnSave()
On Error GoTo errhandler
ThisWorkbook.Save
Application.DisplayAlerts = False
Application.EnableEvents = False
If ThisWorkbook.vfile = "" Then
ThisWorkbook.Save
Else
ThisWorkbook.SaveAs ThisWorkbook.vfile
End If
Application.EnableEvents = True
Exit Sub
errhandler:
Application.EnableEvents = True
End Sub

and in Thisworkbook module

Public vfile 'this need to be declared here

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
tmpString = "Draft.xls" '****RECOMMENDED NAME
Application.DisplayAlerts = False
If ThisWorkbook.Name <> tmpString Then
vfile = Application.GetSaveAsFilename(tmpString)
If vfile <> False Then
Application.EnableEvents = False
ThisWorkbook.SaveAs vfile
Application.EnableEvents = True
Else
vfile = ""
End If
Cancel = True
Else
vfile = ThisWorkbook.Name
End If
End Sub

Regards
keizi

"TW Bake" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>I did, it's under my description of the problem .... help!!!
>
> On Jan 26, 7:18 pm, JLGWhiz <JLGW...@discussions.microsoft.com> wrote:
>> Please post the code that fails.
>>
>>
>>
>> "TW Bake" wrote:
>> > Hi All,

>>
>> > I've taken several stabs at this and am not getting anywhere. When I
>> > use the save button on the toolbar, the BeforeSave event below works as
>> > expected. However, when I envoke a save from another macro (btnSave),
>> > the event does not actually save when the filename is not the
>> > recommended name.

>>
>> > When a macro button is pressed, this event should check if the file
>> > name matches the recommended name (ie Draft1.xls), if not (ie
>> > Draft2.xls) the user shoule be prompted with the SaveAs Dialog box.
>> > The user may or may not rename the file. When the user presses SAVE on
>> > the dialog box, the file should save ... but it does not.

>>
>> > Any help would be appreciated.

>>
>> > thanks,

>>
>> > TW Baker

>>
>> > --------------------------------------------------------
>> > Sub btnSave()
>> > ThisWorkbook.Save
>> > End Sub

>>
>> > Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
>> > Boolean)
>> > tmpString = "Draft1.xls" '****RECOMMENDED NAME
>> > If ThisWorkbook.Name <> tmpString Then
>> > vfile = Application.GetSaveAsFilename(tmpString)
>> > If vfile <> False Then
>> > Application.EnableEvents = False
>> > ThisWorkbook.SaveAs vfile '***This does not happen when
>> > save is called from another macro.
>> > Application.EnableEvents = True
>> > Cancel = True
>> > Else
>> > Cancel = True
>> > End If
>> > End If
>> > End Sub- Hide quoted text -- Show quoted text -

>


 
Reply With Quote
 
TW Bake
Guest
Posts: n/a
 
      31st Jan 2007
On Jan 29, 7:19 pm, "kounoike" <kouno...@nowherembh.nifty.com> wrote:
> i have no idea about why the code "ThisWorkbook.SaveAs vfile" in
> Workbook_BeforeSave does not work when evoked from a macro. this is one
> example to work around, though i'm not sure if this is a right way and works
> in every cases.
>
> in standard module
>
> Sub btnSave()
> On Error GoTo errhandler
> ThisWorkbook.Save
> Application.DisplayAlerts = False
> Application.EnableEvents = False
> If ThisWorkbook.vfile = "" Then
> ThisWorkbook.Save
> Else
> ThisWorkbook.SaveAs ThisWorkbook.vfile
> End If
> Application.EnableEvents = True
> Exit Sub
> errhandler:
> Application.EnableEvents = True
> End Sub
>
> and in Thisworkbook module
>
> Public vfile 'this need to be declared here
>
> Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
> Boolean)
> tmpString = "Draft.xls" '****RECOMMENDED NAME
> Application.DisplayAlerts = False
> If ThisWorkbook.Name <> tmpString Then
> vfile = Application.GetSaveAsFilename(tmpString)
> If vfile <> False Then
> Application.EnableEvents = False
> ThisWorkbook.SaveAs vfile
> Application.EnableEvents = True
> Else
> vfile = ""
> End If
> Cancel = True
> Else
> vfile = ThisWorkbook.Name
> End If
> End Sub
>
> Regards
> keizi
>
> "TW Bake" <twbsoluti...@msn.com> wrote in message
>
> news:(E-Mail Removed)...
>
>
>
> >I did, it's under my description of the problem .... help!!!

>
> > On Jan 26, 7:18 pm, JLGWhiz <JLGW...@discussions.microsoft.com> wrote:
> >> Please post the code that fails.

>
> >> "TW Bake" wrote:
> >> > Hi All,

>
> >> > I've taken several stabs at this and am not getting anywhere. When I
> >> > use the save button on the toolbar, the BeforeSave event below works as
> >> > expected. However, when I envoke a save from another macro (btnSave),
> >> > the event does not actually save when the filename is not the
> >> > recommended name.

>
> >> > When a macro button is pressed, this event should check if the file
> >> > name matches the recommended name (ie Draft1.xls), if not (ie
> >> > Draft2.xls) the user shoule be prompted with the SaveAs Dialog box.
> >> > The user may or may not rename the file. When the user presses SAVE on
> >> > the dialog box, the file should save ... but it does not.

>
> >> > Any help would be appreciated.

>
> >> > thanks,

>
> >> > TW Baker

>
> >> > --------------------------------------------------------
> >> > Sub btnSave()
> >> > ThisWorkbook.Save
> >> > End Sub

>
> >> > Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
> >> > Boolean)
> >> > tmpString = "Draft1.xls" '****RECOMMENDED NAME
> >> > If ThisWorkbook.Name <> tmpString Then
> >> > vfile = Application.GetSaveAsFilename(tmpString)
> >> > If vfile <> False Then
> >> > Application.EnableEvents = False
> >> > ThisWorkbook.SaveAs vfile '***This does not happen when
> >> > save is called from another macro.
> >> > Application.EnableEvents = True
> >> > Cancel = True
> >> > Else
> >> > Cancel = True
> >> > End If
> >> > End If
> >> > End Sub- Hide quoted text -- Show quoted text -- Hide quoted text -

>
> - Show quoted text -


Thanks! I'll give it a try. I'm also going to see about replacing the
GetSaveAs with a GetOpen and see if that makes any difference. Thanks
again!!

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
undo VBA procedure error when called via change event bradmcq Microsoft Excel Programming 8 18th Sep 2009 01:20 PM
Why is a button Click event also called when a textbox TextChanged event is called??? S_K Microsoft ASP .NET 6 8th Nov 2007 07:44 PM
Button Click Event Procedure being called when a Page is refreshed. Kuldeep Microsoft ASP .NET 1 29th May 2007 03:31 PM
Event procedure not being called Duncan Edment Microsoft Access 3 29th Apr 2004 03:48 PM
Save Event - Determine if called by code or user David Sedberry Microsoft Excel Programming 0 2nd Oct 2003 04:56 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:59 AM.