You wrote a macro in the Visual Basic Editor, saved your document, and then reopened it only to find the VBA module completely gone. This happens when you save a file containing VBA code as a .docx file. The .docx format cannot store macros, so Word silently removes all modules during the save operation. This article explains why Word strips VBA code from .docx files, how to recover your lost module if you have not closed the document, and how to prevent the problem by saving in the correct .docm format.
Key Takeaways: Preventing VBA Module Loss When Saving Word Documents
- File > Save As > Save as type > Word Macro-Enabled Document (docm): The only file format that preserves VBA modules in Word
- File > Options > Save > Save files in this format > Word Macro-Enabled Document (docm): Change the default save format to .docm to avoid accidental .docx saves
- Ctrl + Z immediately after saving as .docx: Undo the save operation to recover the VBA module before closing the document
Why Saving as .docx Removes VBA Modules
Word uses three main document file formats that differ in what content they can store. The .docx format is the default standard format introduced in Office 2007. It is designed to store text, images, tables, and formatting. The .docx format does not support VBA macros, ActiveX controls, or form controls. The .docm format is identical to .docx in structure except that it includes a special macros-enabled container that holds VBA projects. The older .doc format also supports macros.
When you choose File > Save As and select Word Document (docx) as the type, Word checks whether the document contains VBA code. If it does, Word displays a warning dialog that says, “The document cannot be saved because it contains macros, ActiveX controls, or other code that cannot be saved in a macro-free document.” The dialog offers two options: Save As a Macro-Free Document or Cancel. If you click Save As a Macro-Free Document, Word removes all VBA modules, class modules, and form modules from the document before writing the file. This action is irreversible once the file is closed.
The removal is not a bug. It is by design to prevent users from accidentally distributing macro-enabled files in environments that block .docm files. However, many users click the warning away without reading it, or they habitually save as .docx and do not notice the warning. The result is a saved file with no code and no way to retrieve the module from the .docx file itself.
Steps to Recover a VBA Module After Saving as .docx
If you saved the document as .docx but have not yet closed it, you can recover the module immediately. If you closed the document after saving, the module is permanently gone from that file and you must restore it from a backup.
Recover the Module Before Closing the Document
- Press Ctrl + Z immediately
This undoes the save operation. The document reverts to its previous state, which still contains the VBA modules. You can verify by pressing Alt + F11 to open the Visual Basic Editor. - Press F12 to open Save As
Do not use Ctrl + S. The Save As dialog lets you choose the correct format. - Change the Save as type to Word Macro-Enabled Document (docm)
In the Save As dialog, click the Save as type dropdown and select Word Macro-Enabled Document (docm). Choose a file name and location, then click Save. - Close and reopen the .docm file
Press Alt + F11 to confirm the VBA module is present. The module tree in the Project Explorer should show the module under the project name.
Restore the Module From a Backup
If you closed the document after saving as .docx, the module is lost from that file. Check these locations for a backup copy:
- File > Info > Manage Document > Recover Unsaved Documents
Word may have an autosaved copy that still contains the module. This works only if an autosave occurred before the .docx save. - Search for a .asd file in the AutoRecover folder
Go to File > Options > Save and note the AutoRecover file location. Open that folder in File Explorer and look for files named after your document with an .asd extension. Open any candidate in Word and check the VBA Editor. - Restore from a previous version in File History or OneDrive
If you have File History enabled or the file was stored in OneDrive, right-click the .docx file in File Explorer, select Properties, go to the Previous Versions tab, and open an earlier version that was saved as .docm.
How to Change the Default Save Format to .docm
To prevent accidental .docx saves in the future, change the default file format to .docm. This affects all new documents.
- Open File > Options
Click File in the ribbon, then click Options at the bottom of the left panel. - Go to the Save category
In the Word Options dialog, click Save in the left sidebar. - Change the default file format
Under Save documents, find the dropdown labeled Save files in this format. Select Word Macro-Enabled Document (docm) from the list. - Click OK
All new documents created with Ctrl + N will now default to .docm. Existing documents still save in their original format unless you change it manually.
If Word Still Shows a Warning When Saving as .docm
You may see a security warning when opening a .docm file from an untrusted location. This is normal. The file still contains the VBA module. To enable the macros, click Enable Content on the yellow security bar below the ribbon. If the module does not appear even after enabling content, the file may have been saved as .docx at some point and the module was removed before you changed the format.
Word Does Not Warn Before Removing Modules
Some users report that Word does not show the macro warning dialog before saving as .docx. This can happen if the VBA project is password-protected or if the document was saved using a macro or add-in that bypasses the dialog. In these cases, the module is removed without any notification. The only safeguard is to always save as .docm when you intend to keep macros.
A .docx File Cannot Be Converted Back to .docm With the Module
Simply renaming the file extension from .docx to .docm does not restore the VBA module. The .docx file does not contain the VBA project data. You must re-create the module from scratch or restore it from a backup. To avoid this, always keep a backup copy of the .docm file before testing or distributing a .docx version.
Word Document Formats: VBA Module Retention Comparison
| Item | .docx | .docm |
|---|---|---|
| Stores VBA modules | No | Yes |
| Stores ActiveX controls | No | Yes |
| Stores form controls | No | Yes |
| Default format in Word 2010 and later | Yes | No |
| Opens in Word Online | Yes | No |
| Shows warning before removing macros | Yes | Not applicable |
| Can be renamed to .docm to regain macros | No | Not applicable |
You can now identify and fix the cause of missing VBA modules after saving in Word. Always save macro-enabled documents as .docm. If you accidentally save as .docx, press Ctrl + Z immediately before closing the file. To change the default save format, go to File > Options > Save and select Word Macro-Enabled Document (docm). Keep a backup of your .docm file before distributing a .docx version to others.