Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Worksheet.Unprotect within WorkbookBeforeSave event fails if Save initiated by VB

I trap the WorkbookBeforeSave event successfully and reliably.

Within that event-handling code, I use the Worksheet.Unprotect method.

In the Workbook object:
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
Cancel = WorkbookBeforeSave(ThisWorkbook, SaveAsUI)
End Sub

In a general module:
Function WorkbookBeforeSave(TargetWorkbook As Workbook, Optional
ByVal SaveAsUI As Boolean) As Boolean
...
TargetWorkbook.Worksheet(1).Unprotect
If TargetWorkbook.Worksheets(1).ProtectContents
Then
i = MsgBox("Failed to unprotect", vbOKOnly,
"DEBUG")
End If
...
End Sub

Unprotect works perfectly reliably when the user initiates the save.

Unprotect reliably fails (with no error message) when the save is
initated by my VB code.
Sub Test()
TargetWorkbook.Save
End Sub

Any idea why? Any work-around?

BTW, I absolutely do not want to disable users' ability to save in the
manner they're familiar with, replacing the usual button with my own
Save command as some have suggested. This application must be easy to
use and compatible with other spreadsheets.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Worksheet.Unprotect within WorkbookBeforeSave event fails if Save initiated by VB

My current solution: If VB wants to save, calls a procedure that
includes:

Sub FileSave(TargetWorkbook as Workbook)
If Not WorkbookBeforeSave(TargetWorkbook ) Then
Application.EnableEvents = False
TargetWorkbook .Save
Application.EnableEvents = True
End If


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 13
Default Worksheet.Unprotect within WorkbookBeforeSave event fails if Save initiated by VB

My current solution: If VB wants to save, calls a procedure that
includes:

Sub FileSave(TargetWorkbook as Workbook)
If Not WorkbookBeforeSave(TargetWorkbook ) Then
Application.EnableEvents = False
TargetWorkbook .Save
Application.EnableEvents = True
End If


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Worksheet.Unprotect within WorkbookBeforeSave event fails if Save

My current solution: If VB wants to save, calls a procedure that
includes:

Sub FileSave(TargetWorkbook as Workbook)
If Not WorkbookBeforeSave(TargetWorkbook ) Then
Application.EnableEvents = False
TargetWorkbook .Save
Application.EnableEvents = True
End If
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
How to determine the worksheet that a calculate event gets initiated on when the workbook is not active Riddler Excel Programming 2 May 16th 07 01:58 PM
Macro to Unprotect save worksheet and protect it again. holy41 Excel Programming 2 July 31st 06 09:47 PM
event after worksheet unprotect sam[_8_] Excel Programming 2 June 29th 06 08:28 AM
Worksheet Save Event ExcelMonkey[_17_] Excel Programming 3 January 24th 04 03:04 PM
Save As - Multiple Sheets fails to save as text file Ravee Srinivasan Excel Programming 2 November 10th 03 04:05 PM


All times are GMT +1. The time now is 03:26 PM.

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

About Us

"It's about Microsoft Excel"