“Invalid Page Fault” message when running a macro

Or: Keeping Word VBA projects clean

Article contributed by Bill Coan, <Jonathan West and Cindy Meister

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 same problem.

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 this size.

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 introduced.

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:

  1. Open the existing developer copy of the template for editing.
  2. Export all code modules, class modules, and the special ThisDocument class module.
  3. Close the developer copy of the template.
  4. Create a new, blank template.
  5. Import all the exported code into the new, blank template.
  6. Copy all the autotext items from the old template into the new one.
  7. Run macros to set up styles, page layout, headers/footers, document variables, custom document properties, commandbars, keybindings.
  8. Insert the content of the old template into the new one.
  9. Save the template as the new developer copy.
  10. Delete the macros that setup styles, page layout, headers/footers, document variables, custom document properties, commandbars, keybindings.
  11. Delete all comments and blank lines from the code.
  12. Save the template as the new user copy.
  13. Compile the code in the user’s copy, digitally sign it, and protect it for viewing.

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.

  1. Click the Zip icon to download the file.
  2. Unzip it.
  3. Double-click the .exe file to install the addin.
  4. 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 version).


Click to view Terms of Use page

Click to view Disclaimer page