MsgBox when saving file. VBA code doesn't work

TimvMechelen

Board Regular
Joined
Nov 7, 2016
Messages
121
Hi all,

Can somebody tell me why this VBA code doesn't work:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI = True Then
MsgBox "Saved", vbOKOnly + vbInformation
ElseIf SaveAsUI = False Then
MsgBox "Not Saved!", vbOKOnly + vbCritical
End If
End Sub

When saving I want a msgbox that say "Saved"
When the saving failed I want a msgbox that says "Not Saved!"

Thanks for your help
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Hi,

Not 100% sure what your requirements are but from looking at your code perhaps this will satisfy them.

As to why your code does not work. I think it is because the trigger is BeforeSave and also I think you are using the SaveAsUI incorrectly.

Code:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
    If Success = False Then MsgBox "Not Saved!", vbOKOnly + vbCritical
    If Success = True Then MsgBox "Saved", vbOKOnly + vbInformation
End Sub

HTH

igold
 
Upvote 0
The event is BeforeSave, so when it runs, it is not known whether the Save failed or not.

I'm not sure how the Save is being initiated (by code, by user selecting Save from the user interface) or why it might fail, but you might use

Code:
If Application.Dialogs(xlDialogSaveAs) Then
    MsgBox "save success"
Else
    MsgBox "save failed"
End If
 
Upvote 0
@Mike

If the user is initiating the save via the user interface would you show me how you would write the code using the BeforeSave event. I tried a couple of times and the code would run and complete before the Save or SaveAs Dialog box popped up.

Thanks in advance for your time.

igold
 
Last edited:
Upvote 0
If the user initiates the Save from the UI, there is no way that the BeforeSave event will notify you of a failed attempt to save. The BeforeSave event runs before the file is saved.

What reasons do you anticipate for the Save failing?
 
Upvote 0
I don't anticipate reasons for the Save failing. But i wanted to replace the MsgBox to something that prevent my .XLSM to overwrite the same file. Thats why i wanted to use the BeforeSave event, and that isn't working.

Thank you for your time
 
Upvote 0
Hi,

Not 100% sure what your requirements are but from looking at your code perhaps this will satisfy them.

As to why your code does not work. I think it is because the trigger is BeforeSave and also I think you are using the SaveAsUI incorrectly.

Code:
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
    If Success = False Then MsgBox "Not Saved!", vbOKOnly + vbCritical
    If Success = True Then MsgBox "Saved", vbOKOnly + vbInformation
End Sub

HTH

igold

When I use your code I get the message, But as I replied to mikerickson
I don't anticipate reasons for the Save failing. But i wanted to replace the MsgBox to something that prevent my .XLSM to overwrite the same file. Thats why i wanted to use the BeforeSave event, and that isn't working.

Thank you for your time

If I change the "Workbook_AfterSave" to "Workook_BeforeSave" it doesn't work anymore.

Thank you for your time
 
Upvote 0
@ mikerickson

Code:
Sub BeforeSave()
    If Application.Dialogs(xlDialogSaveAs) Then
        MsgBox "save success"
        
    Else
        MsgBox "save failed"
        
    End If
    
End Sub

This code doesn't do anything when i press the save button
 
Upvote 0
The Application.Dialogs code was not intended for the BeforeSave event, it was intended to go in a normal sub, but would return whether the Save was successful.

It sounds like your ultimate goal is to prevent the user from saving a workbook unless they change the name.

This code might work for you. Change the "Workbook1.xlsm" to the name of your file.

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim myFullPath As String
    Dim newPath As String
    
    If (ThisWorkbook.Name = "Workbook1.xlsm") And Not (OKFlag) Then
        Rem this is the original workbok
        If SaveAsUI Then
            Cancel = True
            myFullPath = ThisWorkbook.Path & Application.PathSeparator & ThisWorkbook.Name
            newPath = Application.GetSaveAsFilename(myFullPath)
            If newPath = "False" Then Exit Sub: Rem canceled
            If LCase(myFullPath) = LCase(newPath) Then
                MsgBox "You must change the name or location of this file"
            Else
                MsgBox "all good"
                OKFlag = True
                MsgBox myFullPath & vbCr & newPath
                ThisWorkbook.SaveAs newPath
            End If
        Else
            MsgBox "You cannot save this workbook unless you change its name"
            Cancel = True
        End If
        
    End If
    OKFlag = False
End Sub
 
Upvote 0
It sounds like your ultimate goal is to prevent the user from saving a workbook unless they change the name.

This is exactly what i mean yes! I'll try the code in a few minutes, thank you!
 
Upvote 0

Forum statistics

Threads
1,203,739
Messages
6,057,075
Members
444,903
Latest member
Mavericx

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