Use beforesave event to cancel save

gschwint

Board Regular
Joined
Aug 17, 2004
Messages
121
I have created a userform that captures via a public variable if a save of the workbook is desired or not. I attempt to use this public variable result within the workbook beforesave event to either save the workbook or cancel the save (exit the beforesave event). Here is the code that I think should work but it doesn't as the workbook is saved in all instances.

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

Proper_Save.Show 'call userform to get public variable "savequestion" result

If savequestion = True Then 'exit the default save and go back to the program
Cancel = True
Exit Sub
End If

End Sub


I have found that the public variable approach works good and is working properly by returning the correct result (true/false) but the workbook is saved regardless of the result. Any ideas???
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I would like to see how (i.e. the code) the savequestion public variable is loaded.
 
Upvote 0
Here is the code behind the userform.

Code:
Private Sub CommandButton1_Click()

savequestion = True 'don't save

Proper_Save.Hide

End Sub

Private Sub CommandButton2_Click()

response = MsgBox("Are you sure that you want to continue to save in a manner other than what the programmer designer recommends?" & vbNewLine & vbNewLine & "Keep in mind that when the MMP program is changed or updated, this method of saving will require you to re-enter everything you have done to this point, in order to utilize the most current version of the MCPA MMP Spreadsheet!", vbYesNo, "You have been fairly warned!!!")

If response = vbYes Then 'save program
savequestion = False
Proper_Save.Hide
Else 'don't save
savequestion = True
Proper_Save.Hide
End If
End Sub

The variable declaration takes place in a seperate module but isn't used anywhere else. I try to keep my public variables in one location so I would like to leave it there, unless that is the issue. Here is the declaration:

Code:
Public admin, savequestion As Boolean
 
Upvote 0
Well that works for me so it's hard to say but it does seem as though the variable is being destroyed (State Loss perhaps).

Have you tried using a property instead? You are hiding the form rather than unloading it (which is good because the class isn't terminated). So you could use something like:

In the userform class module:
Code:
Private m_blnSave As Boolean

Public Property Let SaveQuestion(ByVal blnSave As Boolean)
    m_blnSave = blnSave
End Property

Public Property Get SaveQuestion() As Boolean
    Me.Show
    SaveQuestion = m_blnSave
End Property

Private Sub CommandButton1_Click()
    With Me
        .SaveQuestion = True
        .Hide
    End With
End Sub

Private Sub CommandButton2_Click()
    response = MsgBox("Are you sure that you want to continue to save in a manner other than what the programmer designer recommends?" & vbNewLine & vbNewLine & "Keep in mind that when the MMP program is changed or updated, this method of saving will require you to re-enter everything you have done to this point, in order to utilize the most current version of the MCPA MMP Spreadsheet!", vbYesNo, "You have been fairly warned!!!")
    
    With Me
        If response = vbYes Then
            .SaveQuestion = False
        Else
            .SaveQuestion = True
        End If
        .Hide
    End With
End Sub

In ThisWorkbook class module:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cancel = Proper_Save.SaveQuestion
End Sub
 
Upvote 0
That still triggers a workbook save no matter the response. I just tried to use a much simplier approach to see if the beforesave event was actually working and it appears that it is not. I used a simple message box (see below) and when the answer is no, the save should cancel, however it does not. Any idea why I am experiencing this issue?

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

response = MsgBox("Are you sure you want to do this?", vbYesNo, "trial")

If response = vbNo Then Cancel = True

End Sub

I just tried this (above coed) on a new workbook using Excel 2007 on a Windows XP machine with no data at all and it did not work there either. It took me to the save-as window where I could then cancel the save. I then tried it using Excel 2000 on a XP machine and it worked just fine. Is it possible this is an issue with the 2007 version of Excel?

.
 
Last edited:
Upvote 0
In the immediate window:
Code:
?Application.EnableEvents
What does this return?

I once had the SAPBEX addin running and it interfered with it. Are you running any addins?
 
Upvote 0
It returns "true"

The only add-ins I have running are those that I have no control over as they are auto loaded on startup at my employer. It is an application for saving info into our electronic records system called OnBase.
 
Upvote 0
I believe one of those addins might be the culprit. I can't think of any other reason why...
 
Upvote 0
I'll see if I can get our IT people to allow me to remove that add-in and then give it a try. Thanks for the help.
 
Upvote 0
- UPDATE -

Problem Solved! The issue was the add-in that was runinng. After disabling it my original code worked just fine.
 
Upvote 0

Forum statistics

Threads
1,224,570
Messages
6,179,611
Members
452,931
Latest member
The Monk

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top