TW VBA starter
Systems Engineer
Hi,
I've stumbled upon the code from 2004 in the thread reference below and it's almost exactly what I'm looking for for my project. Unfortunately, I've ran the code and encountered one main issue. The workbook only closes if you click on don't save. If you click save, it will loop and bring you the safe prompt again. Somehow the "if wbclosing = true then" trigger does not work. I also have the suspicion that after "not saving" the splash screen is the only visible sheet at the point of closing. I've also encountered the issue that if the file is saved with only the splash screen visible, even with macros enabled the Workbook_open sub does not run. I've made sure that all code is stored in the workbook and not module or sheet.
I'm using Office 365, could it be that this type of coding won't work anymore?
Thanks in advance for any suggestions.
Cheers,
thread707-923923
I've stumbled upon the code from 2004 in the thread reference below and it's almost exactly what I'm looking for for my project. Unfortunately, I've ran the code and encountered one main issue. The workbook only closes if you click on don't save. If you click save, it will loop and bring you the safe prompt again. Somehow the "if wbclosing = true then" trigger does not work. I also have the suspicion that after "not saving" the splash screen is the only visible sheet at the point of closing. I've also encountered the issue that if the file is saved with only the splash screen visible, even with macros enabled the Workbook_open sub does not run. I've made sure that all code is stored in the workbook and not module or sheet.
I'm using Office 365, could it be that this type of coding won't work anymore?
Thanks in advance for any suggestions.
Cheers,
thread707-923923
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
'This sub runs when the user tries to close the file
wbClosing = True 'Set the Boolean flag wbXClosing to True. This value is used by SpecialSave sub.
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'This sub runs when the user tries to Save or Save As the file
SpecialSave 'Call the SpecialSave sub
Cancel = True 'Since SpecialSave sub will save the workbook, don't try to repeat it a second time
End Sub
Private Sub SpecialSave()
'This sub runs whenever user tries to save the file
Dim ws As Worksheet, wsSplash As Worksheet, wsCurrent As Worksheet
Dim pswd As String
pswd = "Brad" 'Password to protect the workbook
Application.ScreenUpdating = False 'No screen flicker. Code runs faster if screen doesn't update until macro done
Application.EnableEvents = False 'Stop events handling until end of sub. This prevents recursive calling of event subs.
Set wsSplash = Worksheets("Splash screen") 'Pointer to the Splash screen worksheet
Set wsCurrent = ActiveSheet 'Pointer to the current worksheet
ThisWorkbook.Unprotect Password:=pswd 'Unprotect the workbook using the password stored in string variable pswd
wsSplash.Visible = xlSheetVisible 'Make the Splash screen worksheet visible
For Each ws In ThisWorkbook.Worksheets 'Loop through all the worksheets
If ws.Name <> "Splash screen" Then ws.Visible = xlSheetVeryHidden 'Make the other worksheets very hidden (only visible through VBA code)
Next ws
ThisWorkbook.Protect Password:=pswd 'Protect the workbook before saving it
ThisWorkbook.Save 'Save the file
If wbClosing = True Then ThisWorkbook.Close 'If trying to close the workbook, allow the process to occur
ThisWorkbook.Unprotect Password:=pswd 'Unprotect the workbook. Will get error when trying to make sheets visible if this not done.
For Each ws In ThisWorkbook.Worksheets 'Loop through each worksheet
If ws.Name <> "Splash screen" Then ws.Visible = xlSheetVisible 'Make all sheets except Splash screen visible
Next ws
wsSplash.Visible = xlSheetVeryHidden 'Make Splash screen very hidden
wsCurrent.Activate 'Activate the initial worksheet
ThisWorkbook.Protect Password:=pswd 'Restore workbook protection
Application.EnableEvents = True 'Turn events handling back on
Application.ScreenUpdating = True 'Allow the screen to update
End Sub
Private Sub Workbook_Open()
'This sub runs when the workbook opens
Dim ws As Worksheet, wsSplash As Worksheet
Dim pswd As String
pswd = "Brad" 'Password used to protect the workbook
Application.ScreenUpdating = False 'Turn screen updating off. Sub runs faster. No screen flicker.
Application.EnableEvents = True 'Turn events handling off. Avoids recursive calling of events subs
wbClosing = False 'Initialize wbClosing to False
Set wsSplash = Worksheets("Splash screen") 'Pointer to the Splash screen worksheet
ThisWorkbook.Unprotect Password:=pswd 'Unprotect the workbook
wsSplash.Visible = xlSheetVisible 'Make the Splash screen worksheet visible
For Each ws In ThisWorkbook.Worksheets 'Loop through all the worksheets
If ws.Name <> "Splash screen" Then ws.Visible = xlSheetVisible 'Make worksheets visible
Next ws
wsSplash.Visible = xlSheetVeryHidden 'Make the Splash screen worksheet very hidden (only visible through VBA code)
ThisWorkbook.Protect Password:=pswd 'Protect the workbook with a password
Application.ScreenUpdating = True 'Allow the screen to update
End Sub