Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default WorkbookBeforeClose problem

Hi all,

I am trying to write a WorkbookBefore events so that a custom dialog box
will be shown for specific type of files. Here is the psuedo-code:

Sub App_WorkbookBeforeClose(Wb as Workbook, Cancel as Boolean)
Dim Ans as String

If Wb.Name Like "Book*" Then
Ans = MsgBox("Do you want to save the changes you made to " & wb.Name &
"?"
Select Case Ans
Case vbYes
Call ActualSave Wb, "ABCDE"
Case vbNo
Me.Saved = True
Case vbCancel
Cancel = True
End Select
End if
End Sub

Sub ActualSave(ByVal WhichBook as Workbook, byVal FileName as String)
'Handling saving routine using codes
Filename = Application.GetSaveAsFilename( _
InitialFileName:=DefaultName, _
FileFilter:="Excel Files (*.xls), *.xls")

'Disable all events, including the BeforeSave and BeforeClose events
'in order to prevent infinite loops

If Filename < "False" Then 'User did not cancel the save
Application.EnableEvents = False
On Error Resume Next 'In case the file to be saved already exist
'avoid the error
WhichBook.SaveAs Filename:=Filename, AddToMru:=True
Application.EnableEvents = True
End If
End Sub

Sub App_WorkbookBeforeSave(Wb As Workbook, SaveAsUI as Boolean, Cancel as
Boolean)
Call OtherTask 'Omitted here
End Sub

My problem I encountered is that, on triggering the WorkbookBeforeClose
event and user clicks OK to save a file AND the filename already exists,
then a dialog box asking for replacement will be shown.

If the user responded to this dialog box by clicking "No", then another
dialog box, "Do you want to save the changes you made to "Bookx"" appears,
which is not my intention.

If the user responded "Yes" instead, file will be saved but the file will
not close (which supposedly should have been).

Please advise how can I rectify the situation. Thanks for your help in
advance.

Frederick Chow
Hong Kong.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 156
Default WorkbookBeforeClose problem

try using

Application.DisplayAlerts = false

and

Wb.saved = true
wb.close false


HTH
Philip





"Frederick Chow" wrote:

Hi all,

I am trying to write a WorkbookBefore events so that a custom dialog box
will be shown for specific type of files. Here is the psuedo-code:

Sub App_WorkbookBeforeClose(Wb as Workbook, Cancel as Boolean)
Dim Ans as String

If Wb.Name Like "Book*" Then
Ans = MsgBox("Do you want to save the changes you made to " & wb.Name &
"?"
Select Case Ans
Case vbYes
Call ActualSave Wb, "ABCDE"
Case vbNo
Me.Saved = True
Case vbCancel
Cancel = True
End Select
End if
End Sub

Sub ActualSave(ByVal WhichBook as Workbook, byVal FileName as String)
'Handling saving routine using codes
Filename = Application.GetSaveAsFilename( _
InitialFileName:=DefaultName, _
FileFilter:="Excel Files (*.xls), *.xls")

'Disable all events, including the BeforeSave and BeforeClose events
'in order to prevent infinite loops

If Filename < "False" Then 'User did not cancel the save
Application.EnableEvents = False
On Error Resume Next 'In case the file to be saved already exist
'avoid the error
WhichBook.SaveAs Filename:=Filename, AddToMru:=True
Application.EnableEvents = True
End If
End Sub

Sub App_WorkbookBeforeSave(Wb As Workbook, SaveAsUI as Boolean, Cancel as
Boolean)
Call OtherTask 'Omitted here
End Sub

My problem I encountered is that, on triggering the WorkbookBeforeClose
event and user clicks OK to save a file AND the filename already exists,
then a dialog box asking for replacement will be shown.

If the user responded to this dialog box by clicking "No", then another
dialog box, "Do you want to save the changes you made to "Bookx"" appears,
which is not my intention.

If the user responded "Yes" instead, file will be saved but the file will
not close (which supposedly should have been).

Please advise how can I rectify the situation. Thanks for your help in
advance.

Frederick Chow
Hong Kong.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default WorkbookBeforeClose problem

Thanks for your help, but:

If Wb.Saved = True is inserted, even though the user clicked "Cancel' in
response to asking for replacement, then the file will closed, where the
expected behaviour should be the file remains open.

If Application.DisplayAlerts = False is inserted, then the user will not
have a chance to cancel the choice, which is less than desired.

So any other suggestions?

Frederick Chow
Hong Kong.

"Philip" wrote in message
...
try using

Application.DisplayAlerts = false

and

Wb.saved = true
wb.close false


HTH
Philip





"Frederick Chow" wrote:

Hi all,

I am trying to write a WorkbookBefore events so that a custom dialog box
will be shown for specific type of files. Here is the psuedo-code:

Sub App_WorkbookBeforeClose(Wb as Workbook, Cancel as Boolean)
Dim Ans as String

If Wb.Name Like "Book*" Then
Ans = MsgBox("Do you want to save the changes you made to " & wb.Name &
"?"
Select Case Ans
Case vbYes
Call ActualSave Wb, "ABCDE"
Case vbNo
Me.Saved = True
Case vbCancel
Cancel = True
End Select
End if
End Sub

Sub ActualSave(ByVal WhichBook as Workbook, byVal FileName as String)
'Handling saving routine using codes
Filename = Application.GetSaveAsFilename( _
InitialFileName:=DefaultName, _
FileFilter:="Excel Files (*.xls), *.xls")

'Disable all events, including the BeforeSave and BeforeClose events
'in order to prevent infinite loops

If Filename < "False" Then 'User did not cancel the save
Application.EnableEvents = False
On Error Resume Next 'In case the file to be saved already exist
'avoid the error
WhichBook.SaveAs Filename:=Filename, AddToMru:=True
Application.EnableEvents = True
End If
End Sub

Sub App_WorkbookBeforeSave(Wb As Workbook, SaveAsUI as Boolean, Cancel as
Boolean)
Call OtherTask 'Omitted here
End Sub

My problem I encountered is that, on triggering the WorkbookBeforeClose
event and user clicks OK to save a file AND the filename already exists,
then a dialog box asking for replacement will be shown.

If the user responded to this dialog box by clicking "No", then another
dialog box, "Do you want to save the changes you made to "Bookx""
appears,
which is not my intention.

If the user responded "Yes" instead, file will be saved but the file will
not close (which supposedly should have been).

Please advise how can I rectify the situation. Thanks for your help in
advance.

Frederick Chow
Hong Kong.





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default Problem Solved

Hi Phillip,

Finally I have sought out a way to solve the problem: Add "If Wb.Saved =
False then Exit Sub" statement immediately after the Call ActualSave
command.

Anyway thanks for your attention.

Frederick Chow
Hong Kong.

"Philip" wrote in message
...
try using

Application.DisplayAlerts = false

and

Wb.saved = true
wb.close false


HTH
Philip





"Frederick Chow" wrote:

Hi all,

I am trying to write a WorkbookBefore events so that a custom dialog box
will be shown for specific type of files. Here is the psuedo-code:

Sub App_WorkbookBeforeClose(Wb as Workbook, Cancel as Boolean)
Dim Ans as String

If Wb.Name Like "Book*" Then
Ans = MsgBox("Do you want to save the changes you made to " & wb.Name &
"?"
Select Case Ans
Case vbYes
Call ActualSave Wb, "ABCDE"
Case vbNo
Me.Saved = True
Case vbCancel
Cancel = True
End Select
End if
End Sub

Sub ActualSave(ByVal WhichBook as Workbook, byVal FileName as String)
'Handling saving routine using codes
Filename = Application.GetSaveAsFilename( _
InitialFileName:=DefaultName, _
FileFilter:="Excel Files (*.xls), *.xls")

'Disable all events, including the BeforeSave and BeforeClose events
'in order to prevent infinite loops

If Filename < "False" Then 'User did not cancel the save
Application.EnableEvents = False
On Error Resume Next 'In case the file to be saved already exist
'avoid the error
WhichBook.SaveAs Filename:=Filename, AddToMru:=True
Application.EnableEvents = True
End If
End Sub

Sub App_WorkbookBeforeSave(Wb As Workbook, SaveAsUI as Boolean, Cancel as
Boolean)
Call OtherTask 'Omitted here
End Sub

My problem I encountered is that, on triggering the WorkbookBeforeClose
event and user clicks OK to save a file AND the filename already exists,
then a dialog box asking for replacement will be shown.

If the user responded to this dialog box by clicking "No", then another
dialog box, "Do you want to save the changes you made to "Bookx""
appears,
which is not my intention.

If the user responded "Yes" instead, file will be saved but the file will
not close (which supposedly should have been).

Please advise how can I rectify the situation. Thanks for your help in
advance.

Frederick Chow
Hong Kong.





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
WorkbookBeforeClose event is not canceled Mircea Pleteriu[_2_] Excel Programming 1 June 3rd 05 04:22 AM
question about Auto_Close and WorkbookBeforeClose Brian Murphy Excel Programming 8 October 9th 04 08:08 PM
Unable to save workbook within WorkbookBeforeClose TTN[_3_] Excel Programming 2 November 21st 03 11:11 AM
unable to save current workbook within WorkbookBeforeClose TTN Excel Programming 4 November 20th 03 05:35 PM
Handling WorkbookBeforeClose and WorkbookBeforeSave Tom Ogilvy Excel Programming 0 September 19th 03 06:27 PM


All times are GMT +1. The time now is 08:30 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"