Force users to save as .xlsm or pdf only


New Member
Dec 7, 2019
Office Version
Hello everyone,

This thread is related and inspired on an old one: Force users to save as .xlsm or xltm only.

In any case, I tried to replicate the code for XLSM and PDF format only. No luck. Although the two options are available, the PDF one didn't produce any good file. I would like to ask you if it is possible to make this code work. Thank you very much!

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

Dim txtFileName As String

If SaveAsUI = True Then
Cancel = True
txtFileName = Application.GetSaveAsFilename(fileFilter:="Excel Macro-Enabled Workbook (*.xlsm), *.xlsm, PDF Files (*.pdf), *.pdf", Title:="Save As...")

If txtFileName = False Then
MsgBox "Action Cancelled", vbOKOnly
Cancel = True

Exit Sub
End If

Application.EnableEvents = False
ThisWorkbook.SaveAs Filename:=txtFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
Application.EnableEvents = True

End Sub


Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.


Well-known Member
Feb 26, 2006
I'm just putting this out there.
You cannot save a workbook as a PDF.

You should control what & where the workbook is saved as, instead of using the getsaveas dialog box, it will be quite annoying very quickly.

Watch MrExcel Video

Forum statistics

Latest member

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...