PDA

View Full Version : Disable or enable



Sir Babydum GBE
11-29-2009, 05:53 AM
Hi

How do I write code that, if someone tries to press save it shows a message "Please press apply or cancel before you save" and aborts the save.

Also, can I apply the same principle if someone tries to close the workbook? i.e. "Please press apply or cancel before you close"

Finally, when they press my "apply" or "cancel" macro buttons (pictures with assigned macros) then save and close are re-enabled

Thanks

Bob Phillips
11-29-2009, 06:08 AM
What I think you want to do here BD is to trap the save and close events and invoke your code. That is easily achieved with the following logic



'Caters for both Save and SaveAs

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Dim sFile
Application.EnableEvents = False
'<Optional - this would be before save code>
Cancel = True
If SaveAsUI Then
sFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls")
If sFile <> False Then
ThisWorkbook.SaveAs sFile
'<Optional - this would be after save code>
End If
Else
ThisWorkbook.Save
'<Optional - this would be after save code>
End If
Application.EnableEvents = True
End Sub

There are 3 places that you can inject your specific code here.

To achieve your custom actions you would probably be best to build a userform that mimics a MsgBox with custom text (I must build one of these), and then trap those click events, and then save the workbook (or close if that path).

Another consideration is whether the Apply or Cancel steps could have been already been effected elsewhere, in which case you just exit gracefully. You may or may not have this capability elsewhere, i.e. it may not be relevant.

Sir Babydum GBE
11-30-2009, 12:45 PM
Thanks very much Mr X, I'll do my best to employ your suggestions!