Macro-Enabled Template VBA Code for Saving Workbook

letswriteafairytale

New Member
Joined
Dec 23, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
So, I have macro-enabled templates for office use. I found a VBA code to KINDA address the problem I was having.

Not everyone here is very Excel savvy, so I know they wouldn't be able to remember to save the workbooks as macro-enabled workbook.

I found the code for that:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Right(ThisWorkbook.Name, 4) <> "xlsm" Then
Dim txtFileName As String
MsgBox "This workbook must be saved as a macro enabled workbook"
Cancel = True
txtFileName = Application.GetSaveAsFilename(ThisWorkbook.FullName, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm", , "Save As XLSM file")
If txtFileName = "False" Then
MsgBox "Action Cancelled", vbOKOnly
Cancel = True
Exit Sub
End If
Application.EnableEvents = False
ThisWorkbook.SaveAs Filename:=txtFileName, FileFormat:=52 'xlOpenXMLWorkbookMacroEnabled
Application.EnableEvents = True
End If
End Sub




We save these as PDF's as well. So, when they're finished with the workbook, we will save as PDF. The problem with this code is it ONLY allows .xlsm.... I am no coding expert 😅 so, what do I add to the code to allow the save as PDF, as an option as well?





Thanks!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You'll need to add the following code. Edit the path as desired for your location.

VBA Code:
'Create and assign variables
Dim saveLocation As String
saveLocation = "C:\Users\marks\OneDrive\Documents\myPDFFile.pdf" '... <-- edit path here

'Save active workbook as PDF
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=saveLocation
 
Upvote 0

Forum statistics

Threads
1,224,858
Messages
6,181,431
Members
453,040
Latest member
Santero

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