VBA & Macro Problems

How to Set Duplex Printing in Word VBA7 and 64-Bit Office

Rana GarciaRana Garcia Nov 2, 2026 869 views

Question details

The user needs to configure a printer for duplex printing via Word automation using VBA7 in a 64-bit Office environment.

How to Set Duplex Printing in Word Using VBA7 and 64-Bit Office
Product
Microsoft Word
Device & OS
Windows
Scenario
Automating document printing on both sides using VBA macros in a modern 64-bit Office setup.
Observed behavior
Classic 32-bit Windows API declarations fail in 64-bit environments, requiring updated pointer and printer structure data types to manipulate printer settings successfully.
Before you start

Ensure you have developer access to modify VBA macros and verify that your physical printer hardware actually supports automatic two-sided (duplex) printing.

Solution 1Recommended

Update VBA Declarations for 64-Bit Compatibility

Update your Windows API declarations using PtrSafe and LongPtr to handle 64-bit memory addresses correctly when configuring printer settings.

In 64-bit Office environments, standard 32-bit API declarations will result in type mismatch or overflow errors. You must update your API calls to use VBA7 syntax, specifically implementing the PtrSafe keyword and the LongPtr data type to safely handle 64-bit memory pointers.

1
Declare API functions with PtrSafe

Open your VBA editor and add the 'PtrSafe' keyword to your Windows API 'Declare' statements (e.g., 'Declare PtrSafe Function OpenPrinter...') to indicate to the compiler that the function is safe for 64-bit execution.

2
Use LongPtr for handles and pointers

Replace the standard 'Long' data type with 'LongPtr' for all printer handles (like hPrinter) and pointer fields within your structure definitions. This ensures variables are large enough to store 64-bit addresses without truncation.

3
Retrieve and modify the DEVMODE structure

Use the DocumentProperties API to fetch the current printer's DEVMODE structure. Locate the dmDuplex property within this structure and change its value to 2 (for short-edge binding) or 3 (for long-edge binding).

4
Apply updated printer settings

Write the modified DEVMODE structure back to the printer configuration using the DocumentProperties and SetPrinter API functions to finalize the duplex printing requirement.

Update VBA Declarations for 64-Bit Compatibility
Testing Your Macro: Always test updated 64-bit macros on a virtual printer (like Microsoft Print to PDF) first to ensure memory pointers are handling the DEVMODE structure without crashing the application.
Advanced Macro Support

Automate and Print Efficiently with WPS Writer

WPS Office provides robust support for VBA macros, allowing you to seamlessly execute advanced automation scripts like duplex printing. It offers a familiar interface and excellent compatibility with your existing Word macro projects.

  1. 1. Open WPS Writer: Launch WPS Writer and open the document you wish to print or automate.
  2. 2. Access the Developer Tab: Navigate to the Developer tab on the top ribbon to access the built-in VBA editor and macro tools.
  3. 3. Run Your Macro: Paste your updated 64-bit compatible VBA code into the module and execute it to automate your double-sided printing tasks.
High compatibility with Microsoft Word documents and standard VBA scriptsNative support for complex document formatting and printing configurationsLightweight application footprint with high performance on WindowsFamiliar Developer tab and macro editor interface for quick transitions
microsoft office alternative - wps office

Frequently Asked Questions

What does the PtrSafe keyword mean in VBA7?

PtrSafe is a keyword introduced in VBA7 that asserts a Declare statement is safe to run in 64-bit versions of Office. It is strictly required when calling external Windows API functions in a 64-bit environment to prevent compilation errors.

Why do I get a 'Type Mismatch' error with my old printer macros in 64-bit Word?

This error occurs because older 32-bit macros typically use the 'Long' data type for memory pointers. In 64-bit Office, a 'Long' is too small to hold a 64-bit memory address. You must replace 'Long' with 'LongPtr' for all handles and pointers.

What are the DEVMODE dmDuplex values for double-sided printing?

In the Windows API DEVMODE structure, setting dmDuplex to 1 means simplex (single-sided printing). Setting it to 2 enables duplex printing with short-edge binding (flip up), while 3 enables duplex with long-edge binding (flip over).

Can I force duplex printing without using VBA macros?

Yes. You can manually configure duplex printing by pressing Ctrl+P to open the Print menu, clicking on 'Printer Properties', and selecting the 2-sided printing option in your specific printer's driver settings.