Getting Userform Command Buttons to Work with a BeforeSave VBA Macro

letswriteafairytale

New Member
Joined
Dec 23, 2024
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I have created a userform with 3 buttons, "Save as .XLSM", "Save as .PDF" and "Cancel"

What I would like is for this command box to pop up when we go to save the document (click on save as > browse)

The buttons work just fine every time someone hits Save as .PDF.

BUT, it will only work once if someone chooses save as .XLSM, it will save as .xlsm, then when you go to save again, the save as dialog box opens, but doesn't actually ever save again.

Here is the BeforeSave VBA:

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel as Boolean)
    Cancel = True
    UserForm1.Show
End Sub

Here is the UserForm1 code:

VBA Code:
Private Sub CommandButton1_Click()
Call Save_as_XLSM
End Sub

Private Sub CommandButton2_Click()
Call Save_as_PDF
End Sub

Private Sub CommandButton3_Click()
Call Cancel
End Sub

Private Sub Label1_Click()

End Sub

Private Sub Save_as_XLSM()
 Dim ws As Sheet1
    Dim filename As String
    Dim saveAsDialog
    Dim savePath As Variant

    Set ws = ThisWorkbook.ActiveSheet

saveAsDialog = Application.GetSaveAsFilename( _
    filefilter:="Macro-Enabled Workbook (*.xlsm), *xlsm", InitialFileName:="", Title:="Please choose location to save this document")
    
  If saveAsDialog <> False Then
        ActiveWorkbook.SaveAs filename:=saveAsDialog, FileFormat:=52
        Exit Sub
    End If
Unload Me
End Sub


Private Sub Save_as_PDF()
Dim ws As Sheet1
    Dim filename As String
    Dim saveAsDialog
    Dim savePath As Variant
 
 Set ws = ThisWorkbook.ActiveSheet

saveAsDialog = Application.GetSaveAsFilename( _
    filefilter:="PDF Files (*.pdf), *pdf", InitialFileName:="", Title:="Please choose location to save this document")

  If saveAsDialog <> False Then
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=saveAsDialog, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
        Exit Sub
    End If
Unload Me
End Sub

Private Sub Cancel()
Unload Me
    End
End Sub

Private Sub Label2_Click()

End Sub

Private Sub UserForm_Click()

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Paste in ThisWorkbook module :

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cancel = True
    UserForm1.Show
End Sub

Paste in a Regular module :

Code:
Private Sub Save_as_XLSM()
    Dim saveAsDialog As Variant

    saveAsDialog = Application.GetSaveAsFilename( _
        FileFilter:="Macro-Enabled Workbook (*.xlsm), *.xlsm", _
        InitialFileName:="", _
        Title:="Please choose location to save this document")
    
    If saveAsDialog <> False Then
        Application.DisplayAlerts = False ' Suppress confirmation dialog
        ActiveWorkbook.SaveAs Filename:=saveAsDialog, FileFormat:=52
        Application.DisplayAlerts = True
        ThisWorkbook.Saved = True ' Mark workbook as saved
    End If
    Unload Me
End Sub

Untested here. Let me know if it works.
 
Upvote 0
Paste in ThisWorkbook module :

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cancel = True
    UserForm1.Show
End Sub

Paste in a Regular module :

Code:
Private Sub Save_as_XLSM()
    Dim saveAsDialog As Variant

    saveAsDialog = Application.GetSaveAsFilename( _
        FileFilter:="Macro-Enabled Workbook (*.xlsm), *.xlsm", _
        InitialFileName:="", _
        Title:="Please choose location to save this document")
   
    If saveAsDialog <> False Then
        Application.DisplayAlerts = False ' Suppress confirmation dialog
        ActiveWorkbook.SaveAs Filename:=saveAsDialog, FileFormat:=52
        Application.DisplayAlerts = True
        ThisWorkbook.Saved = True ' Mark workbook as saved
    End If
    Unload Me
End Sub

Untested here. Let me know if it works.
Tried it in the UserForm & a regular module (wasn't sure if thats what you meant by "regular module", so I did both), didn't work. Same issue, save as dialog comes up and lets me try and save, but nothing actually saves.
 
Upvote 0
Please post a copy of your workbook to a download site (i.e., Dropbox.com) . Do not include confidential information.
Provide link for download.
 
Upvote 0
Please post a copy of your workbook to a download site (i.e., Dropbox.com) . Do not include confidential information.
Provide link for download.

Not sure if it could be the issues, but there is a hidden sheet.

I had to remove the beforesave VBA in order to save, so that will need to be added back in.



Thanks!
 
Upvote 0
This is the basic coding for saving as .xlsm .

VBA Code:
Private Sub Save_as_XLSM()
    With Application.FileDialog(msoFileDialogSaveAs)
        .FilterIndex = 2
        If .Show Then
            ActiveWorkbook.SaveAs filename:=.SelectedItems(1), _
                FileFormat:=xlOpenXMLWorkbookMacroEnabled
        End If
    End With
End Sub

It works for multiple savings one at a time.
 
Upvote 0
Solution
This is the basic coding for saving as .xlsm .

VBA Code:
Private Sub Save_as_XLSM()
    With Application.FileDialog(msoFileDialogSaveAs)
        .FilterIndex = 2
        If .Show Then
            ActiveWorkbook.SaveAs filename:=.SelectedItems(1), _
                FileFormat:=xlOpenXMLWorkbookMacroEnabled
        End If
    End With
End Sub

It works for multiple savings one at a time.
That works!

Thank you. Last question. Do you know what code to use to prevent autosave from automatically turning on in the new save?
 
Upvote 0
Hmmm ... I don't see that anywhere in your code. Where was it located ?

I believe you would want to remove this macro completely :

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cancel = True
    UserForm1.Show
End Sub
 
Upvote 0
Hmmm ... I don't see that anywhere in your code. Where was it located ?

I believe you would want to remove this macro completely :

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Cancel = True
    UserForm1.Show
End Sub
Okay, yeah it does seem to be a problem with the UserForm IN the BeforeSave Macro. Because the command box works just fine when testing the UserForm. Its when its being called at the BeforeSave where the issues happen.

There's no way to correct that? All I want is to be able to ONLY have 2 save as options (or 3 options if you include XLTM) for XLSM or PDF.

I've tried a few codes before making the UserForm, but only the XLSM would work, not the PDF. I don't NEED a UserForm, I just thought that would be the solution to get both options.
 
Upvote 0

Forum statistics

Threads
1,225,315
Messages
6,184,237
Members
453,223
Latest member
Ignition04

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