You see the Word VBA compile error “Sub or Function Not Defined” after moving a macro project from one computer to another or from an older version of Office. This error means the Visual Basic for Applications editor cannot locate a procedure that your code calls. The root cause is almost always a missing reference or a broken library path that was valid on the source system but not on the destination system. This article explains how to identify the missing reference, repair the project, and prevent the error from recurring on future migrations.
Key Takeaways: Fixing the VBA Compile Error After Project Migration
- Tools > References in the VBA editor: Shows all active library references; a missing reference displays “MISSING” in the dialog and is the usual cause of the error.
- Early binding vs. late binding: Switching from early binding (specific library reference) to late binding (CreateObject) eliminates dependency on a specific library version.
- Export and reimport modules: Removes corrupted project metadata that can cause reference resolution failures after migration.
Why the “Sub or Function Not Defined” Error Occurs After Migration
When you write VBA code that calls a function or sub from an external library — for example, Excel.Application, MSForms.UserForm, or Scripting.FileSystemObject — Word stores a reference to that library. This reference includes a GUID and a version number. On the source machine, the library is present. On the target machine, the library may be a different version, a different language pack, or missing entirely. The VBA editor cannot resolve the reference, so it marks every call to that library as undefined.
A second common cause is a corrupted or orphaned reference that survived the migration. When you copy a .dotm or .docm file, the embedded VBA project carries its reference table with it. If the target system has a different Office edition — for instance, 64-bit vs. 32-bit — the library paths change and the references break.
The Role of Early Binding in Migration Failures
Early binding means your code declares object variables with a specific type, such as Dim xlApp As Excel.Application. This requires a reference to the Excel object library. If the target machine does not have Excel installed, or if the library version differs, the compile fails. Late binding uses Dim xlApp As Object and CreateObject("Excel.Application"), which resolves at runtime and does not require a compile-time reference.
Steps to Identify and Fix the Missing Reference
Follow these steps in order. Stop after each step if the error no longer appears.
Step 1: Open the VBA Editor and Check References
- Open the VBA editor
Press Alt+F11 in Word. The VBA editor window opens. - Open the References dialog
In the VBA editor menu, select Tools > References. The References dialog lists all active libraries. - Find the missing reference
Scroll through the list. A missing reference shows the word “MISSING” at the start of its entry. The checkbox is still checked. Uncheck that entry and click OK. - Test the project
Press F5 to run the macro. If the error is gone, the missing reference was the cause. If the error persists, continue to Step 2.
Step 2: Re-add the Correct Library Reference
- Open the References dialog again
Press Alt+F11 and select Tools > References. - Locate the correct library
Find the library you just unchecked. For example, “Microsoft Excel 16.0 Object Library” or “Microsoft Scripting Runtime.” Check its box. - Verify the file path
With the library selected, look at the Location field at the bottom of the dialog. The path should point to a file that exists on the target system. If the path is broken, browse to the correct file using the Browse button. - Click OK and compile
Click OK. In the VBA editor, select Debug > Compile VBAProject. If no errors appear, the fix is complete.
Step 3: Convert Early-Bound Code to Late Binding
If the missing reference is to a library that is not installed on the target system — for example, Excel on a machine that has only Word — convert the relevant code to late binding. This is the most reliable long-term fix for migrated projects.
- Identify early-bound declarations
In the VBA editor, search forAskeywords followed by a library type, such asAs Excel.ApplicationorAs Scripting.FileSystemObject. These are early-bound declarations. - Replace with Object type
ChangeDim xlApp As Excel.ApplicationtoDim xlApp As Object. - Replace New keyword with CreateObject
ChangeSet xlApp = New Excel.ApplicationtoSet xlApp = CreateObject("Excel.Application"). - Remove the library reference
Open Tools > References and uncheck the library you no longer need. Compile the project with Debug > Compile VBAProject. The error should be gone.
Step 4: Export and Reimport All Modules
If the error persists despite correct references, the VBA project itself may contain corrupted metadata. Exporting and reimporting modules rebuilds the project file.
- Export each module
In the Project Explorer pane, right-click a module and select Export File. Save the .bas file to a folder. Repeat for every module, class module, and user form. - Remove the original modules
Right-click each module and select Remove. Confirm the removal. Do not save changes to the exported files. - Import the modules back
Right-click the project in Project Explorer, select Import File, and select the .bas files you exported. Repeat for all modules. - Re-add references and compile
Set the required references in Tools > References. Compile the project. The error should be resolved.
If the Error Still Occurs After These Fixes
Word VBA Compile Error on a 64-Bit vs. 32-Bit Migration
If you moved the project from a 32-bit Office to a 64-bit Office, declare API functions with the PtrSafe keyword. Without PtrSafe, 64-bit Office treats the declaration as undefined. Add PtrSafe after Private Declare or Public Declare. Also update Long parameters used for pointers to LongPtr.
Word VBA Compile Error Caused by an Orphaned Reference to a Missing Add-In
Some projects reference libraries from third-party add-ins. If the add-in is not installed on the target machine, the reference appears as MISSING. Uncheck the orphaned reference in Tools > References. If your code depends on that add-in, install the add-in on the target machine or rewrite the code to avoid the dependency.
Word VBA Compile Error After Upgrading Office Version
An Office upgrade can change library version numbers. For example, “Microsoft Word 15.0 Object Library” changes to “Microsoft Word 16.0 Object Library.” Open Tools > References, uncheck the old version, and check the new version. The compile error will disappear.
Early Binding vs. Late Binding for Migrated Projects
| Item | Early Binding | Late Binding |
|---|---|---|
| Library reference required | Yes, checked in References dialog | No, resolved at runtime |
| Compile-time error on missing lib | Yes, “Sub or Function Not Defined” | No, runtime error only if lib missing |
| IntelliSense support | Yes, full autocomplete and type checking | No, no autocomplete during editing |
| Performance | Slightly faster at runtime | Slightly slower at runtime |
| Best for migrated projects | Only if target environment is identical | Recommended for any project that moves between machines |
You can now identify and fix the “Sub or Function Not Defined” compile error in a migrated Word VBA project. Start by checking the References dialog for MISSING entries and re-adding the correct library. For projects that move between different Office versions or configurations, convert early-bound code to late binding. As a final cleanup, export and reimport all modules to remove corrupted metadata. The most effective long-term strategy is to use late binding for any external object references such as Excel, Outlook, or the File System Object.