LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #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.


 
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 10:00 PM.

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

About Us

"It's about Microsoft Excel"