Thread: Cancel = True
View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Vergel Adriano Vergel Adriano is offline
external usenet poster
 
Posts: 857
Default Cancel = True

Hello Tanya,

Cancel=True in the BeforeSave event means cancel the save operation. If
Cancel=False, Excel will proceed with saving your file at the end of the
event. You need to have Cancel=True because you had already saved the
workbook on the previous line and there's no need for Excel to save it again.
Furthermore, if you take that line out, Excel will end up saving your
workbook after you unhide the sheets.

Try your BeforeSave code like this:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Dim strSaveAs As String

Cancel = True

If SaveAsUI = True Then
'get a save as filename from the user
strSaveAs = Application.GetSaveAsFilename(Me.Name, "Excel Files
(*.xls), *.xls", 1)
If strSaveAs = "False" Then
'user cancelled the save as operation
Exit Sub
End If
End If

'code to hide certain sheets

Application.EnableEvents = False
If SaveAsUI = True Then
Me.SaveAs strSaveAs
Else
Me.Save
End If
Application.EnableEvents = True


'code to unhide certain sheets


End Sub



--
Hope that helps.

Vergel Adriano


"JustBreathe" wrote:

First of all, thanks everyone for the invaluable help you've provided me on
this message board! ...now....

code:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'code to hide certain sheets
Me.Save
Cancel = True
'code to unhide certain sheets
End Sub

question: what exactly does the Cancel = True do (or not do)?
I found that when I was trying to Save As a new filename that I was not
being given the option to change the name. when I comment out the Cancel =
True line in my BeforeSave procedure I am able to do save with a new filename
when doing a Save As. I want to understand why this is so, so that I know
that I am not inadvertently causing other potential problems by removing the
Cancel = True.

TIA
Tanya (vba newbie)