A
Alex
I'm using the following code for saving on closing.
But, when I click Yes to save changes it's not saving it.
Could anybody help?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim logReadOnly As Boolean
'Turn off events to prevent unwanted loops
Application.EnableEvents = False
logReadOnly = False
'Evaluate if workbook is saved and emulate default propmts
With ThisWorkbook
If Not .Saved Then
Select Case MsgBox("Do you want to save the changes you made to
'" & .Name & "'?", _
vbYesNoCancel + vbExclamation)
Case Is = vbYes
'Call customized save routine
If ActiveWorkbook.ReadOnly Then
logReadOnly = True
MsgBox ("The Application is read-only. You cannot save
changes.")
Else
Call CustomSave
End If
Case Is = vbNo
'Do not save
logReadOnly = True
Case Is = vbCancel
'Set up procedure to cancel close
Cancel = True
End Select
End If
'If Cancel was clicked, turn events back on and cancel close,
'otherwise close the workbook without saving further changes
If Not Cancel = True Then
.Saved = True
Application.EnableEvents = True
If logReadOnly Then
.Close savechanges:=False
Else
.Close savechanges:=True
End If
Else
Application.EnableEvents = True
End If
End With
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False
'Call customized save routine and set workbook's saved property to true
'(To cancel regular saving)
Call CustomSave(SaveAsUI)
Cancel = True
'Turn events back on an set saved property to true
Application.EnableEvents = True
ThisWorkbook.Saved = True
End Sub
But, when I click Yes to save changes it's not saving it.
Could anybody help?
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim logReadOnly As Boolean
'Turn off events to prevent unwanted loops
Application.EnableEvents = False
logReadOnly = False
'Evaluate if workbook is saved and emulate default propmts
With ThisWorkbook
If Not .Saved Then
Select Case MsgBox("Do you want to save the changes you made to
'" & .Name & "'?", _
vbYesNoCancel + vbExclamation)
Case Is = vbYes
'Call customized save routine
If ActiveWorkbook.ReadOnly Then
logReadOnly = True
MsgBox ("The Application is read-only. You cannot save
changes.")
Else
Call CustomSave
End If
Case Is = vbNo
'Do not save
logReadOnly = True
Case Is = vbCancel
'Set up procedure to cancel close
Cancel = True
End Select
End If
'If Cancel was clicked, turn events back on and cancel close,
'otherwise close the workbook without saving further changes
If Not Cancel = True Then
.Saved = True
Application.EnableEvents = True
If logReadOnly Then
.Close savechanges:=False
Else
.Close savechanges:=True
End If
Else
Application.EnableEvents = True
End If
End With
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Turn off events to prevent unwanted loops
Application.EnableEvents = False
'Call customized save routine and set workbook's saved property to true
'(To cancel regular saving)
Call CustomSave(SaveAsUI)
Cancel = True
'Turn events back on an set saved property to true
Application.EnableEvents = True
ThisWorkbook.Saved = True
End Sub