In Excel VBA, opening a print dialog programmatically can save time when you need to preview or print specific sheets without manual clicks. The simplest way is to use the `Application.PrintDialog` macro, which shows a standard print preview with all available settings (paper size, orientation, margins, etc.) directly from your code. This avoids typing `Ctrl+P` repeatedly and integrates seamlessly into automated workflows.
What You'll Need
- A Microsoft Excel workbook (any version from Excel 2010 onward)
- Access to the Excel VBA editor (press
Alt+F11to open it) - A module where your VBA code will reside (right-click the
VBAProject (YourWorkbook)>Insert > Module)
Step-by-Step
-
Open the VBA editor:
Press
Alt+F11to launch the Excel VBA editor. If prompted, ensure you’re editing the correct workbook by selecting it in the dropdown at the top. -
Insert a new module:
In the VBA editor, go to
Insert>Module. This will create an empty module where you can write your code. -
Paste the print dialog code:
Insert the following VBA code into the module. This line sets up the `PrintDialog` object to show a preview window with all print settings:
Sub ShowPrintDialog() Dim printDialog As PrintDialog Set printDialog = Application.PrintDialog End SubSave the module before running it. -
Run the macro:
Press
F5or go toRun>Run Sub/UserFormto execute the macro. The dialog will open immediately, allowing you to adjust print settings (paper size, margins, orientation) before printing. If the dialog doesn’t appear, check that no other macros are interfering. -
Optional: Print specific sheets or ranges:
To target a single sheet, modify the code like this:
Sub PrintSelectedSheet() Dim ws As Worksheet Set ws = ActiveSheet ' Change this to your sheet name: Set ws = ThisWorkbook.Sheets("Sheet1") If ws.PrintOut Copies:=1, OutputType:=xlPrintCurrentView Then MsgBox "Print job submitted successfully!" Else MsgBox "No print preview available for this sheet." End IfKey differences here: -PrintOutsends a direct print job (bypass preview). -OutputType:=xlPrintCurrentViewforces a print preview. Test this in a module with a sheet named "Sheet1" (or replace it with your sheet name).
Common Mistakes
- Macro fails to run: If the dialog doesn’t appear, check for: - Typos in the code (e.g., misspelled `PrintDialog`). - Conflicting macros—run a single macro at a time. - Excel in full-screen mode—exit full-screen before running.
-
PrintDialog opens blank or empty:
Ensure you’re working in a
Workbookmodule, not aUserForm. The example above is correct for this purpose. - No sheets selected: If you omit `ActiveSheet` or `ThisWorkbook.Sheets("Sheet1")`, Excel defaults to the active sheet. Always qualify the sheet name explicitly if needed.
Tips
- Preview vs. Direct Printing: Use `PrintDialog` for interactive preview (adjust margins, orientation). For one-time printing, replace it with `PrintOut Copies:=1`.
-
Multiple Worksheets:
Loop through sheets and print each with:
For Each ws In ThisWorkbook.Sheets If ws.Name <> "HiddenSheet" Then ' Skip hidden sheets ws.PrintOut OutputType:=xlPrintCurrentView End If Next ws -
Save the Print Settings:
Store default settings in variables for reuse:
Dim defaultPrintDialog As PrintDialog Set defaultPrintDialog = Application.PrintDialog defaultPrintDialog.AllowResizing = False ' Disable resizing -
Debugging:
Add error handling to catch issues:
On Error Resume Next Set printDialog = Application.PrintDialog If Err.Number <> 0 Then MsgBox "Print dialog failed. Error: " & Err.Description End If
Troubleshooting
If the code doesn’t work as expected, try these steps in order:
- Verify the macro name: Ensure the macro name is exactly `ShowPrintDialog` (case-sensitive). Rename it if needed.
-
Check Excel settings:
Go to
File > Options > Advancedand ensure "Enable all Excel features" is checked. If you see "Excel’s default print area settings are not recorded," it may block the preview. - Test in a new workbook: Open a fresh Excel file and run the macro. Corrupted workbooks can interfere.
-
Update Excel:
Run
Help > Check for Updatesto ensure you’re using the latest version. -
Reinstall VBA:
If all else fails, close Excel and run:
Control Panel > Programs > Turn Windows features on or off> Check "Microsoft Visual C++ 2015-2022 Redistributable" and "Visual Basic for Applications" (under Microsoft Office Tools). Restart Excel afterward.
Frequently Asked Questions
Can I use this in Office 365?
Yes, the `PrintDialog` method works identically in Office 365 (Excel 2019/2021) as in older versions. Microsoft’s UI is consistent across versions.
Why won’t the dialog show margins or other settings?
The `PrintDialog` object respects all print settings by default. If you see a blank dialog, ensure no macros are modifying `ActiveSheet.PrintArea` or `PrintTitleRow` before running the macro.
How do I print only specific rows/columns?
Use `PrintOut` with a filter or range:
Sheets("Sheet1").Range("A1:D10").Copy
Worksheets("PrintList").PasteSpecial xlPasteValues ' Create a temporary sheet with the data
Sheets("PrintList").PrintOut
Note: This requires a secondary sheet for printing.
Can I customize the paper size in VBA?
Yes, set the paper size before showing the dialog:
Application.PrintDialog.PaperSize = xlUSLetter ' Valid values: xlLetter, xlLegal, xlExecutive, etc.
Set printDialog = Application.PrintDialog
List of valid constants: xlA3, xlB4, xlLegal, xlLetter (check `Application.PaperSizes` for full list).
What if I get a "Run-time error 438" (Object doesn’t support this property)?
This typically occurs if:
- You’re using an older Excel version (2013 or earlier) without `Application.PrintDialog`.
- A macro is modifying the `PrintDialog` object before your code runs.
Try this alternative:
Sub PrintWithPreview()
ActiveWindow.DisplayGrid = False ' Disable gridlines
ActiveSheet.PrintPreview
End Sub
How do I prevent the print dialog from resizing?
Add this line to your `PrintDialog` setup:
Application.PrintDialog.AllowResizing = False
This forces the dialog to stay at its original size.
Microsoft’s reference for PrintDialog provides more details.