PC Review


Reply
Thread Tools Rate Thread

BeforeSave, Cancel and ReadOnly

 
 
Steve Hunter
Guest
Posts: n/a
 
      23rd Apr 2007
Hello

I am using the BeforeSave event to replace the normal save action with one
that determines changed data and writes it to a database. The basic structure
is as follows:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "This would be the custom save action"
Cancel = True
End Sub

This works fine except when the workbook is read-only, which I require to
prevent users from messing around with the xls file, deleting it accidently
or whatever. Then it displays the message:

'savetest.xls' is read-only. To save a copy, click OK, then give the
workbook a new name in the Save As dialog box.

and continues as it should do with the custom action.

How do I get it to stop displaying that message box? It's entirely useless in
this case and just confuses people.

Thanks
Steve

 
Reply With Quote
 
 
 
 
Steve Hunter
Guest
Posts: n/a
 
      23rd Apr 2007
Ah it's ok I think I found a solution:


' In a module:

Public Sub ReplacementSave()
MsgBox "This is the replacement save subroutine"
End Sub


' In ThisWorkbook:

Private Sub Workbook_Activate()
For Each C In Application.CommandBars.FindControls(ID:=3)
C.OnAction = "ReplacementSave"
Next
Application.MacroOptions Macro:="ReplacementSave", HasShortCutKey:=True, ShortcutKey:="s"
End Sub

Private Sub Workbook_Deactivate()
For Each C In Application.CommandBars.FindControls(ID:=3)
C.OnAction = ""
Next
Application.MacroOptions Macro:="ReplacementSave", HasShortCutKey:=False
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "This message should not have appeared"
Cancel = True
End Sub

 
Reply With Quote
 
Jim Rech
Guest
Posts: n/a
 
      23rd Apr 2007
Users will be prompted to save changes if they try to close the workbook or
close Excel and Excel thinks changes have been made, so you might need to
add this to the ThisWorkbook module:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
ReplacementSave
Saved = True
End Sub


--
Jim
"Steve Hunter" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
| Ah it's ok I think I found a solution:
|
|
| ' In a module:
|
| Public Sub ReplacementSave()
| MsgBox "This is the replacement save subroutine"
| End Sub
|
|
| ' In ThisWorkbook:
|
| Private Sub Workbook_Activate()
| For Each C In Application.CommandBars.FindControls(ID:=3)
| C.OnAction = "ReplacementSave"
| Next
| Application.MacroOptions Macro:="ReplacementSave",
HasShortCutKey:=True, ShortcutKey:="s"
| End Sub
|
| Private Sub Workbook_Deactivate()
| For Each C In Application.CommandBars.FindControls(ID:=3)
| C.OnAction = ""
| Next
| Application.MacroOptions Macro:="ReplacementSave",
HasShortCutKey:=False
| End Sub
|
| Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As
Boolean)
| MsgBox "This message should not have appeared"
| Cancel = True
| End Sub
|


 
Reply With Quote
 
dq
Guest
Posts: n/a
 
      23rd Apr 2007
I think the simplest solution would be to put the line
Application.Displayalerts = False
in the WorkBook_Open event to surpress the errormessage as the saving
doesn't seem to be a problem

 
Reply With Quote
 
Steve Hunter
Guest
Posts: n/a
 
      23rd Apr 2007
Thanks, that works fine. I noticed that without trapping the BeforeClose event
the save before close dialog would just keep popping back up again and again
when clicking Yes.


"Jim Rech" <(E-Mail Removed)> wrote:
> Users will be prompted to save changes if they try to close the workbook or
> close Excel and Excel thinks changes have been made, so you might need to
> add this to the ThisWorkbook module:
>
> Private Sub Workbook_BeforeClose(Cancel As Boolean)
> ReplacementSave
> Saved = True
> End Sub


 
Reply With Quote
 
Steve Hunter
Guest
Posts: n/a
 
      23rd Apr 2007
That does work but it affects all other workbooks open in Excel too, where I
may want to see the alerts.


dq <(E-Mail Removed)> wrote:
> I think the simplest solution would be to put the line
> Application.Displayalerts = False
> in the WorkBook_Open event to surpress the errormessage as the saving
> doesn't seem to be a problem

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel WB opened from Sharepoint as ReadOnly, ReadONLY for WB = FAL Barb Reinhardt Microsoft Excel Programming 0 19th Jan 2010 10:44 PM
datagridviewcolumn.readonly not really readonly boo@gmail.com Microsoft VB .NET 0 31st May 2007 01:42 AM
How to use Excel VBA to cancel Excel readonly status =?Utf-8?B?a29iZXRpbmc=?= Microsoft Excel Programming 2 17th Mar 2006 06:03 PM
Textbox readonly -> but not with readonly property Milosz - [playseven.com] Microsoft Dot NET Compact Framework 3 1st Mar 2004 12:27 PM
Thoughts on readonly collection of readonly elements emma middlebrook Microsoft C# .NET 0 11th Jan 2004 06:42 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:02 AM.