Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Workbook_BeforeSave not behaving as expected

Try the following code in the thisworkbook module.

Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ChAction As Variant
If ThisWorkbook.Saved Then Exit Sub
ChAction = MsgBox("Do you want to save the changes you made to '"
& ThisWorkbook.Name & "'?", vbYesNoCancel + vbExclamation)
Select Case ChAction
Case vbCancel
Cancel = True
Case vbYes
SafeSave True
Case vbNo
ThisWorkbook.Saved = True
End Select
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
If SaveAsUI Then Exit Sub
SafeSave
Cancel = True
End Sub

Private Sub SafeSave(Optional bClose As Boolean = False)

'!!!your saving code here!!!

With Application
.EnableEvents = False
ThisWorkbook.Save
.EnableEvents = True
.ThisWorkbook.Saved = True
If bClose Then ThisWorkbook.Close False
End With
End Sub

By the way, you cannot secure highly confidential information in a
spreadsheet by this route as its pretty easy to unprotect sheets and
unhide cells. If it really is important that there is no unauthorised
access of the information then if you continue to use Excel you will
need to encrypt the confidential information as well.

Peter Grebenik

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Workbook_BeforeSave question Frederick Chow Excel Programming 0 March 21st 06 07:24 PM
Workbook_BeforeSave() bmm Excel Programming 3 August 16th 04 03:37 PM
Workbook_Beforesave getting breached Hari[_3_] Excel Programming 2 June 11th 04 10:57 PM
Workbook_BeforeSave Bill Oertell Excel Programming 5 December 21st 03 07:33 PM
Workbook_BeforeSave() in xla Bent Kjeldsen Excel Programming 6 September 24th 03 01:49 PM


All times are GMT +1. The time now is 06:27 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"