“Invalid Page Fault” message when running a macro
Keeping Word VBA projects clean
Article contributed by Bill
Coan, <Jonathan West
Word templates get bloated through repeated editing and saving of the VBA
code. Bloated templates can often throw invalid page faults for no apparent
reason when running VBA code.
Why Word doesn't clean up after itself when saving a template which contains
a VBA project is one of the great mysteries of life. Excel suffers from the
The way to fix this is to export and remove all the modules from the
template, and then save the template. (Take a backup first!) Then re-import all
the modules again, save the template, recompile and save the template again. If
you have been editing the VBA over a long period, and saving many times, you
may be astonished at how much smaller the new template is.
How often should you clean a project? No one can answer that question for
you. Each project is different. Larger projects generally need to be cleaned
more often than smaller projects. One rule of thumb is to clean a project
whenever it has grown to 1.5 times its normal size but this is a rough
guideline only. Very large projects should be cleaned well before they reach
The Word Code Cleaner cannot remove bloat caused by extensive customizations
of Command Bars or Key Bindings. If a project contains substantial
customization of this sort, or if you frequently modify the customization and
save the changes, the project will eventually become corrupt, even if you do
regularly clean it with the Word Code Cleaner.
Nor can the Word Code Cleaner remove bloat caused by proliferation of unused
List Templates in a document or template. Any modifications to the numbering
scheme in a document or template can quickly cause List Templates to
proliferate and get propagated to new documents.
The only way to avoid the bloat caused by customizations of Command Bars and
Key Bindings and proliferation of unused List Templates is to recreate a
project from scratch, starting with a new blank template that has never had its
Command Bars or Key Bindings customized and never had any unused List Templates
Serious developers may want to consider generating styles, including
numbering, via code, so that styles can be recreated quickly when recreating a
project from scratch. The same goes for generating page setup, including
headers and footers, and for generating document variables and custom document
properties. By doing all these things via code, you’ll save hours of work every
time you decide that it’s time to recreate a project from scratch.
In my own experience, I’ve found that it is helpful to maintain both a
“user” and a “developer” copy of each project. The developer copy contains all
of the code for the project, including the code for recreating the project from
scratch. The user copy doesn’t contain the code for recreating the project.
This allows the user copy to be kept as small as possible.
Use the following steps to modify a project:
- Open the existing developer copy of the template for editing.
- Export all code modules, class modules, and the special ThisDocument
- Close the developer copy of the template.
- Create a new, blank template.
- Import all the exported code into the new, blank template.
- Copy all the autotext items from the old template into the new one.
- Run macros to set up styles, page layout, headers/footers, document
variables, custom document properties, commandbars, keybindings.
- Insert the content of the old template into the new one.
- Save the template as the new developer copy.
- Delete the macros that setup styles, page layout, headers/footers,
document variables, custom document properties, commandbars, keybindings.
- Delete all comments and blank lines from the code.
- Save the template as the new user copy.
- Compile the code in the user’s copy, digitally sign it, and protect it
This can be a long and involved process. The Word Code Cleaner
(see below) automates the portion involving export and import of code modules. In addition to cleaning your project this way, the Word Code
Cleaner can optionally strip comments, blank lines, and spaces from your code.
This allows you to achieve the minimum possible file size for your project.
Obtaining the Word Code Cleaner
The Word Code Cleaner was originally written by Rob Bovey for Excel, then
modified to run in Word by Robert Affleck, and has since been updated by
Bill Coan. It will work with Word 97, Word 2000 and Word 2002. The Word Code
Cleaner does not work with any version of Word for Macintosh.
the Zip icon to download the file.
- Unzip it.
- Double-click the .exe file to install the addin.
- Start Word and choose Tools|Word Code Cleaner.
If the Word Code Cleaner doesn't fix your problem, have a look at
WD2002: Part 1:
Troubleshooting Problems When Word Has Encountered a Problem and Needs to Closewhich provides
very detailed information on how to track down the causes of Office IPFs (any