logo
search
VBA & Macro Problems

Excel VBA Macro to Print to a Specific Printer and Paper Size

Maira MehtabMaira Mehtab Sep 22, 2026 873 views

Question details

The user needs an Excel VBA macro that can assign a specific printer, customize paper sizes (such as statement or letter), and export selected ranges to PDF without reusing previous print settings.

Product
Excel
Device & OS
not provided
Scenario
Creating automated buttons in a cashout workbook to quickly print specific selected ranges to designated printers and export as PDF with exact paper dimensions.
Observed behavior
The current macros default to the last used printer settings instead of applying the intended custom printer and paper size rules.
Before you start

Ensure your target printer is properly installed, turned on, and connected to your computer. Note that printer naming conventions and custom paper size parameters (like statement size) can vary widely depending on your specific Windows printer driver.

Solution 1Recommended

Configure Page Setup and Active Printer via VBA

Define the print area, page orientation, paper size, and destination printer directly in your VBA code before executing the print command.

To prevent Excel from reusing the last active printer settings, you must explicitly declare the Application.ActivePrinter property and configure the PageSetup properties for your worksheet before calling PrintOut.

1
Open the VBA Editor

Press Alt + F11 in Excel to open the Visual Basic for Applications editor, then insert a new Module from the Insert menu.

2
Define the Page Setup

Use the Worksheets("Sheet1").PageSetup object to set .PrintArea = "A1:D20", .Orientation = xlPortrait, and .PaperSize = xlPaperStatement.

3
Set the Active Printer

Assign the specific printer network path or name using a command like Application.ActivePrinter = "Your Printer Name on Ne01:".

4
Execute the Print Command

Call the Range("A1:D20").PrintOut method to send the configured job directly to the designated printer.

Finding Your Exact Printer Port: Printer names in VBA must match the exact string found in Windows devices, including the port (e.g., 'HP LaserJet on Ne01:'). You can find this by recording a macro while manually switching printers.

Easily Run VBA Macros and Print with WPS Spreadsheet

WPS Spreadsheet offers robust support for VBA macros, allowing you to automate complex printing tasks, define specific paper sizes, and export to PDF seamlessly without altering your existing scripts.

  1. 1. Enable Macros: Open WPS Spreadsheet, go to the Developer tab, and enable macros to access the VBA environment.
  2. 2. Edit your Script: Click on 'Visual Basic' to paste or modify your existing print and PDF export macros.
  3. 3. Run the Automation: Assign your macros to worksheet buttons and click them to automatically print to your specific printer.
Highly compatible with Microsoft Excel VBA scripts, macros, and file formats.Built-in PDF export tool requires no additional plugins or virtual printers.Lightweight software with a familiar interface for a seamless transition.Advanced print configuration options for customized document output.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA macro print to the wrong printer?

If Application.ActivePrinter is not explicitly defined in your macro code before the PrintOut method is called, the program will default to the last used printer. You must specify the exact printer name and port in your script.

How do I find the correct printer port for my VBA code?

You can easily find the correct string by recording a new macro. Start recording, open the Print dialog, select your desired printer, and stop recording. Open the VBA editor to view the exact printer string (e.g., 'PrinterName on Ne02:') generated by Excel.

What is the VBA constant for statement-size paper?

The standard VBA constant is xlPaperStatement. However, if your specific printer driver does not natively support this mapping, you might need to assign a custom paper size ID integer that is unique to your driver's configuration.

Can I export to PDF without changing my default printer?

Yes. Using the ExportAsFixedFormat method in VBA saves the range or worksheet as a PDF directly. This operation operates independently of the ActivePrinter property, leaving your print spooler defaults untouched.