Wednesday, November 29, 2006

BeforeSave only with Save, not Save As

http://blog.livedoor.jp/andrewe/archives/cat_415504.html

I see this question asked a few times and just as often answered...anyway I figure I'd include it here too.

Q. When you use a BeforeSave event, is there a way to stop it from firing when using Save As?

A. Yep, use a variation of this code. This example asks the user whether they really want to save, ie they have pushed Save, not Save As which you use to give a workbook a (new) filename.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI = False Then
If MsgBox("Do you really want to save this Workbook? ", vbYesNo) = vbNo Then
Cancel = True
Exit Sub
End If
End If
End Sub

It's the SaveAsUI part that exits the sub. Keep in mind that the code also exits when working with unsaved workbooks, saving them would involve using a new filename (ie Save As).

No comments: