PDA

View Full Version : [SLEEPER:] Multiple commands in BeforeClose



dfenton21
03-19-2009, 02:53 PM
I have my file set up so that it displays one sheet if its opened with macros disabled. I am trying to add another commend into the BeforeClose sub that saves the workbook if the file is not read only. If it is read only, it should just close without a prompt for "Do you want to save..." or "A file named xxx already exists...".

I think the problem is that when the workbook is closed a change is made (hiding/unhiding) so excel prompts with a Save box.

I am a novice to VBA, but I have tried to get it to work. Can someone help please. This is the code I have currently:



Private Sub Workbook_BeforeClose(Cancel As Boolean)
Sheets("Warning").Visible = True
Sheets("Report").Visible = xlVeryHidden
If ActiveWorkbook.ReadOnly Then
Application.DisplayAlerts = False
Else
ActiveWorkbook.Save
End If
End Sub

Instead of Application.DisplayAlerts = False, I have also used ThisWorkbook.Saved=True

Thanks

Bob Phillips
03-19-2009, 03:03 PM
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim sFile
Application.EnableEvents = False
Cancel = True
Sheets("Warning").Visible = True
Sheets("Report").Visible = xlVeryHidden
If ActiveWorkbook.ReadOnly Then
Application.DisplayAlerts = False
Else
ActiveWorkbook.Save
End If
Application.EnableEvents = True
End Sub

dfenton21
03-19-2009, 03:20 PM
Thanks very much, but it is still not working.

Perhaps I should have given you more details. I have a drawing object with Application.Quit assigned to it. The user would use that to close the spreadsheet, but the File menu or the x button.