ThisWorkbook.Save not working under Workbook_BeforeSave, Help!

M

Michel

Why doesn't the workbook get saved here ?

Private Sub Workbook_BeforeSave(ByVal SaveAsUi As Boolean, Cancel As
Boolean)
Application.EnableEvents = False
ThisWorkbook.Worksheets("Sales").Protect Password:="123",
UserInterfaceOnly:=True, Scenarios:=True, Contents:=True
ThisWorkbook.Save
ThisWorkbook.Worksheets("Sales").Unprotect Password:="123"
Cancel = True
Application.EnableEvents = True
End Sub

This sub get's called with ThisWorkbook.Save and debug does step
through it all the way, only it doesn't get saved?!

Thanks.
 
F

Frank Kabel

Hi Michel
code works for me. How do you recognize that this won't save your file?
 
B

Bob Phillips

Because you have set Cancel = True, which is telling the event to cancel the
save.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
F

Frank Kabel

Hi Bob
but he saves the workbook manually (with disabling the events prior to
this so the BeforeSave event is not called again).
 
B

Bob Phillips

I must admit to not trying it, just saw the Cancel = True.

Testing it, it also works for me now.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top