How to Use Conditional Compilation in Word VBA for Multiple Office Versions
🔍 WiseChecker

How to Use Conditional Compilation in Word VBA for Multiple Office Versions

When you write VBA macros in Word, the code may behave differently depending on the Office version (2013, 2016, 2019, Microsoft 365). Features like the new Comment API or the Document Inspector changed between versions. Without version detection, your macro will crash or produce wrong results on unsupported versions. Conditional compilation lets you write one macro that runs the correct code block for each Office version automatically.

This article explains how conditional compilation works in Word VBA, how to set up version-specific constants, and how to write macros that stay compatible across Office 2013 through Microsoft 365. You will learn the correct syntax for #If…Then…#Else blocks and how to test your code without errors.

Key Takeaways: Conditional Compilation for Multi-Version Word VBA

  • #If VBA7 Then … #End If: Detects 64-bit Office (VBA7) versus 32-bit Office (VBA6) to use correct Declare statements.
  • #If Win64 Then … #End If: Detects 64-bit Windows to handle pointer-safe API calls.
  • Custom conditional constant with #Const: Lets you define your own version flag (e.g., Office2019) and compile only the code for that version.

ADVERTISEMENT

How Conditional Compilation Works in Word VBA

Conditional compilation uses compiler directives that the VBA editor evaluates before running the code. The directives are #If, #ElseIf, #Else, and #End If. Unlike runtime If...Then statements, conditional compilation physically excludes the inactive code blocks from the compiled binary. This means the macro runs faster and will not fail on unsupported versions.

The VBA editor provides two built-in constants: VBA7 and Win64. VBA7 is True when the host application uses VBA version 7, which started with Office 2010 64-bit. Win64 is True only when the operating system is 64-bit Windows. You can also define your own constants using #Const at the top of a module or in the project properties under Conditional Compilation Arguments.

Prerequisites for Using Conditional Compilation

You need access to the VBA editor (Alt+F11) and a Word document or template that contains macros. The conditional directives work in standard modules, class modules, and form code modules. No special add-ins or permissions are required. However, you must test the compiled macro on each target Office version to confirm the correct code block runs.

Steps to Set Up Conditional Compilation in Word VBA

Follow these steps to add conditional compilation to an existing macro or to write a new version-safe macro.

  1. Open the VBA Editor and locate your macro
    Press Alt+F11 in Word to open the VBA editor. In the Project Explorer, double-click the module that contains your macro. If you do not have a module yet, right-click the project, choose Insert > Module.
  2. Define a conditional compilation constant for Office version
    At the top of the module, add a #Const line. For example: #Const AppVersion = 16 for Office 2016, 2019, or Microsoft 365. Use 15 for Office 2013. This constant will be used in #If blocks.
  3. Wrap version-specific code inside #If…#End If
    Write the code that works on Office 2016 or later inside #If AppVersion = 16 Then. Write the fallback code for Office 2013 inside #Else. Close with #End If. Example:
    #If AppVersion = 16 Then
        ' Use new Comments.Add2 method
        ActiveDocument.Comments.Add2 Range:=Selection.Range, Text:="Review"
    #Else
        ' Use old Comments.Add method
        ActiveDocument.Comments.Add Range:=Selection.Range, Text:="Review"
    #End If
  4. Add 64-bit detection for API Declare statements
    If your macro uses Windows API functions, wrap the Declare statement inside #If VBA7 Then. For 64-bit Office, use PtrSafe keyword. For 32-bit, omit it. Example:
    #If VBA7 Then
        Private Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long
    #Else
        Private Declare Function GetTickCount Lib "kernel32" () As Long
    #End If
  5. Test the macro on each target Office version
    Compile the macro (Debug > Compile VBA Project) on one version. Then copy the document or template to a computer running a different Office version. Run the macro and verify that the correct code block executes. If you defined the constant manually, change its value and recompile before testing the other version.

Using Project-Level Conditional Compilation Arguments

Instead of #Const at the top of a module, you can set the constant globally for the entire VBA project. In the VBA editor, click Tools > Project Properties. On the General tab, type the constant in the Conditional Compilation Arguments field. For example: AppVersion = 16. Separate multiple constants with colons: AppVersion = 16: DebugMode = 1. This method keeps the constant out of your code and makes it easy to change for testing.

ADVERTISEMENT

Common Mistakes When Using Conditional Compilation

“Compile Error: Else Without If”

This error occurs when the #Else or #End If is misspelled or missing. Check that every #If has a matching #End If. The directives must be on separate lines and start with the hash symbol at the beginning of the line.

“User-Defined Type Not Defined” on a Different Version

If you use a custom type that exists only in newer Office versions, wrap the entire Type...End Type block inside a conditional compilation block. Otherwise the compiler will always try to compile it and fail on older versions.

Conditional Constant Not Evaluated at Design Time

When you use #Const inside a module, the constant value is evaluated only when that module is compiled. If you change the constant in another module, the first module still uses the old value. To avoid confusion, define all version constants in one standard module or use project-level arguments.

Forgetting to Recompile After Changing the Constant

Changing the #Const value does not automatically recompile the project. You must run Debug > Compile VBA Project to apply the change. If you do not recompile, the old compiled code runs and the conditional blocks do not update.

Conditional Compilation vs Runtime Version Detection

Item Conditional Compilation (#If) Runtime Detection (If…Then)
When code is evaluated At compile time At runtime
Inactive code in compiled binary Excluded entirely Still present but not executed
Error prevention on unsupported version Prevents compile errors Cannot prevent compile errors from API Declare or missing objects
Performance impact None Small overhead from version check
Best use case API Declare statements, object model differences that cause compile errors Minor behavior differences that do not affect compilation

Conditional compilation is the only safe way to handle differences that cause compile errors, such as missing methods or 32-bit versus 64-bit API declarations. Runtime detection works for runtime-only differences like default property values or minor UI changes.

You can now write Word VBA macros that work on Office 2013, 2016, 2019, and Microsoft 365 without separate code files. Use #Const or Project Properties to define your version flag, wrap version-specific code in #If...#End If blocks, and always recompile after changing the constant. For API-heavy macros, add #If VBA7 Then with PtrSafe to support 64-bit Office. This approach keeps your code clean, reduces maintenance work, and prevents crashes on unsupported versions.

ADVERTISEMENT