Hi All,
I'm working on a macro process that forces users to log in by only showing a landing page when they open the workbook.
In short, when you save:
This works fine if users save changes, and then closes the workbook.
The problem is if there are unsaved changes, and the user tries to close the workbook:
"Do you want to save changes" appears - fine. The user can choose to Save, and the macros run as they should.
But then "Do you want to save changes" appears again in an infinite loop, until the user chooses Don't Save. This happens even if ThisWorkbook.Saved is set to True. I'm not sure what other sort of flag I could set to make the message not appear.
These are the macros:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'---If this is not the first use of the workbook, cancel the save
If Not pFirstUse = True Then
Cancel = True
'---Lock and save the workbook
mLockAndSave
'---If the user is logged in, unlock the workbook after saving
If pUser <> "" Then
mUnlock
End If
End If
End Sub
Sub mUnlock()
Dim aWorksheet
Application.ScreenUpdating = False
For Each aWorksheet In ThisWorkbook.Worksheets
If aWorksheet.Name <> Range("aaUsers").Parent.Name Then
aWorksheet.Visible = xlSheetVisible
End If
Next
Application.ScreenUpdating = True
ThisWorkbook.Saved = True
End Sub
Sub mLockAndSave()
Dim aWorksheet
Application.ScreenUpdating = False
Worksheets(Range("aaWelcome").Parent.Name).Visible = xlSheetVisible
For Each aWorksheet In ThisWorkbook.Worksheets
If aWorksheet.Name <> Range("aaWelcome").Parent.Name Then
aWorksheet.Visible = xlVeryHidden
End If
Next
Application.EnableEvents = False
ThisWorkbook.Save
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
I'm working on a macro process that forces users to log in by only showing a landing page when they open the workbook.
In short, when you save:
- Workbook_BeforeSave event cancels the save and runs two macros, mLockAndSave, and mUnlock instead
- mLockAndSave hides all worksheets except the landing page
- mLockAndSave disables events, saves the workbook, and reenables events
- mUnlock shows all the normal worksheets (so that the user can keep working) and sets the ThisWorkbook.Saved property to True
This works fine if users save changes, and then closes the workbook.
The problem is if there are unsaved changes, and the user tries to close the workbook:
"Do you want to save changes" appears - fine. The user can choose to Save, and the macros run as they should.
But then "Do you want to save changes" appears again in an infinite loop, until the user chooses Don't Save. This happens even if ThisWorkbook.Saved is set to True. I'm not sure what other sort of flag I could set to make the message not appear.
These are the macros:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'---If this is not the first use of the workbook, cancel the save
If Not pFirstUse = True Then
Cancel = True
'---Lock and save the workbook
mLockAndSave
'---If the user is logged in, unlock the workbook after saving
If pUser <> "" Then
mUnlock
End If
End If
End Sub
Sub mUnlock()
Dim aWorksheet
Application.ScreenUpdating = False
For Each aWorksheet In ThisWorkbook.Worksheets
If aWorksheet.Name <> Range("aaUsers").Parent.Name Then
aWorksheet.Visible = xlSheetVisible
End If
Next
Application.ScreenUpdating = True
ThisWorkbook.Saved = True
End Sub
Sub mLockAndSave()
Dim aWorksheet
Application.ScreenUpdating = False
Worksheets(Range("aaWelcome").Parent.Name).Visible = xlSheetVisible
For Each aWorksheet In ThisWorkbook.Worksheets
If aWorksheet.Name <> Range("aaWelcome").Parent.Name Then
aWorksheet.Visible = xlVeryHidden
End If
Next
Application.EnableEvents = False
ThisWorkbook.Save
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub