View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
dq dq is offline
external usenet poster
 
Posts: 46
Default Workbook_BeforeSave not behaving as expected

Hi,

To protect some highly confidential data in a workbook I want to make
sure this data is always hidden and the worksheet protected before
saving so it is never visible in the saved version. So I wrote the
following code:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, ByRef
Cancel As Boolean)
' Check if the offending lines are visible. If not exit
If (shWork.Range("rCost").EntireRow.Hidden) Then Exit Sub

Application.ScreenUpdating = False
Application.EnableEvents = False

Call HideCosts ' Hide the offending lines
Call Me.Save ' Save the workbook
Call ShowCosts(True) ' Show the lines again
Me.Saved = True ' Showing the lines made the workbook
'unsaved' again

Application.EnableEvents = True
Application.ScreenUpdating = True
Cancel = True ' We already saved the workbook
End Sub

When I save the workbook using the 'Save' command (menu, toolbar,
programmatically) it works fine. The rows are hidden and the book is
protected. But if I want to close the workbook when it is not yet
saved, I get into a loop. Excel asks me if I want to save, I say yes
and the book is saved. Then it asks me again and again. When I say no
it simply closes but it will be very confusing to the people who'll be
using this workbook.

Has anybody an idea why I get in this loop. When the function exits,
the property 'Saved' is true, so I don't see why Excel keeps asking if
I want to save.
P.S. There is nothing in the Befor_Close event that changes anything
in the workbook.

Thanks in advance.

DQ