Troubleshooting Word 2007 Documents More Easily Using VBA
Summary: This article provides an overview with examples of best practices for troubleshooting Word 2007 documents, along with tips for using VBA to help simplify troubleshooting. (6 printed pages)
Stephanie Krieger, arouet.net, Microsoft Office MVP
Applies to: Microsoft Office Word 2007
No More Trial and Error
Microsoft Office Word documents corrupt all the time, don't they? Well, actually no, they don't. Office Word documents, especially Word 2007 documents, rarely corrupt. But, documents are misdiagnosed as corrupt all the time.
When a document behaves badly or is difficult to format, how can you tell whether the cause is actually document corruption, a corrupt object in the document, conflict with an add-in, or even just incorrect formatting?
For this article, your inner power user is just as important as your interest in using VBA to simplify troubleshooting and save time. Effective document troubleshooting must start with your experience creating documents and your understanding of how to use the features in the Word user interface (UI). Only then can VBA effectively help you diagnose or fix your document.
This article explores some common reasons that documents are perceived to be corrupt and how to troubleshoot them effectively using features available from the UI and, when appropriate, VBA.
When it comes to troubleshooting documents, someone with considerable experience creating documents who knows nothing about programming is likely to have an easier time making effective use of VBA than a professional developer who never creates complex Word documents. However, in order to focus on topics relevant to troubleshooting, this article does assume that you have at least some familiarity with VBA basics, including how to record a macro, write a simple macro, and use core elements of the Visual Basic Editor (VBE). For help filling in some blanks or reviewing basics, see Additional Resources.
Best Practices for Document Troubleshooting
Before VBA can be of help with troubleshooting, you need to have some idea of what you're dealing with. VBA as a troubleshooting tool can provide information that you can't get through the UI and can often make cleanup faster. So, when you know what information to look for or what type of fix you need, your efforts are much more effective. Following are just two quick steps to help you narrow down the issue:
Try to determine if the problem is related to the integrity of content, program settings, or formatting.
Does the document cause Word to stop working? (i.e., does it freeze or crash?)
If the answer to this question is yes, move onto step two.
Does some document content fail to appear in one or more views?
If so, there is most likely nothing wrong with the document. Check your settings in Word Options to confirm that your display settings are correct. For example, you may have unchecked the option to display drawing objects and text boxes or checked the option to show picture placeholders.
Does formatting fail to work correctly?
Some content may appear differently on screen than printed, may not behave correctly when you try to apply formatting, or may appear to provide inconsistent results.
If so, what type of formatting does not work correctly? Is the problem with styles, numbered lists, tables, or page or section layout? Usually these issues are cumbersome formatting choices but there may be issues with the integrity of content as well. Move on to step two.
Use Open and Repair to try to diagnose the issue.
In the Open dialog box, select your file and then, instead of clicking Open, click the arrow beside the open button and then click Open and Repair.
When you open a document in Word 2007 using this method, a copy of your document is opened and the original is unaffected. If integrity errors are found in any of your content, a dialog box (as shown in Figure 1) appears to show you the type of error found.
Figure 1. The Show Repairs dialog box
This dialog box can be a troubleshooter's best friend, but there are a few things to know about the information it provides:
The repairs mentioned in the dialog box will not necessarily resolve the issue. Do not assume that the document is fixed, but use the information provided about the type of error to troubleshoot and resolve it.
For example, if the issue is a numbered style, Open and Repair may remove the bullet or number from the style. But, the underlying style properties may still have issues (and may be related to the paragraph properties listed here as well). The easiest solution in this case is to replace the existing numbered style with a new one.
Another common example that does not appear in this case is Shape Properties. Floating objects can be complex to manage in Word documents. But, if your document contains this type of error the problem may even be a shape you can't see, such as objects created when you paste content from other sources.
In the case of a corrupt table (referred to by the Table End-of-Cell Markers listings), you may need to replace the table with a new one. But, remember that the solution may be simpler than you expect. For example, if the table has the Text Wrap Around setting enabled, try copying the content of the corrupt table into a new table shell that does not use that setting and then delete the original; if the table is nested, try creating a new host table (the outermost table in the nesting layers) and then copy the nested tables and other content into that host.
The Go To option in this dialog box can only take you to certain types of content. For example, it cannot take you to the style or paragraph issues but can take to the referenced tables.
Using the information that you did (or did not) get from Open and Repair, you can now form a hypothesis and, if appropriate, enlist the help of VBA to go further. If Open and Repair did not find any issues, consider if the problem could be related to the way the document is formatted.
When the document is not displaying instability (that is, it's not crashing) and you don't see a formatting or setting-related reason for the issue, consider whether what you believe to be a problem could possibly a mistaken expectation about how a feature works. Word is powerful software with a lot of capabilities. No matter how well you know the program, you can still encounter unfamiliar behavior.
Using the Immediate Window to Reduce Trial and Error
If you use the Immediate Window in the VBE, you know that it can be a great way to get information about your document or perform individual tasks that are either difficult or impossible to do from within the UI. If that's news to you, learn how to use this tool in the article Extending Word 2007, Excel 2007, and PowerPoint 2007 with a Single Line of VBA Code.
The Immediate Window is a perfect troubleshooting tool because using one line of code at a time helps you solve problems incrementally. It also may provide information that tells you whether the right approach is to write a macro or to use features in the UI to fix your issue. Consider the following example:
Open and Repair indicates that one or more shapes in the document has an integrity issue, but you don't see any shapes in the document. This may result from the fact that you may have a shape with no fill or border, a shape may be off of the page, or it may be hidden behind other shapes.
To resolve this issue, first use the Immediate Window to ask Word how many shapes it finds in the document.
If you get an answer of one or more, select the first shape using the following code:
Then, press Alt+F11 to toggle back to the document window and then press CTRL+X to cut the active selection. Create a new document and then paste the cut content. You can then look at the shape to see if it is required in the document. And, if not, delete it.
If more than one shape was found, repeat steps 2 and 3 as needed.
Of course, use common sense here. If many shapes are found in step one, consider writing a macro to move them all at once to a new document.
If the answer in step 1 is zero, or taking the preceding steps does not fix the problem, Open and Repair was not wrong. Remember that the shapes may be in a different document story and the question you asked related to the main document story only. In fact, check your document for Different First Page or Different Odd and Even headers that may have been previously used and then turned off. Remember that content you add to headers and footers remains in the document unless you delete it, even if you set those headers and footers to not be visible.
Using Macros to Get Information or Reduce Clean-Up Time
When the problem in the document is about ill-behaving, overcomplicated or simply bad formatting , you may be able to use a few simple macros to do a lot of the clean-up work. For help getting that done, see the article Using VBA to Format Long Word 2007 Documents in a Fraction of the Time. In that article, you can learn how to use loops, conditional statements, and grouping structures to accomplish more with your macros in less time.
Additionally, you can use macros to get information from your document that requires a more complex question than you're able to ask in the immediate window. For example, if you're having difficulty with paragraph styles in your document, you may want to know how what paragraph styles and how many are in use. But, as noted in the next section of this article, simply asking how many styles may not be enough information to help you. Instead, try something like the following macro.
Sub CheckMyStyles() 'Declare integer variables for your loop and the counter in the loop Dim i As Integer, iP As Integer 'Set the counter variable to zero iP = 0 With ActiveDocument For i = 1 To .Styles.Count With .Styles(i) 'Check to see if each style is a paragraph style in use. If .Type = wdStyleTypeParagraph And .InUse = True Then 'If the condition is true, print the style name in the immediate window. Debug.Print .NameLocal 'If the condition is true, increase the counter by 1. iP = iP + 1 End If End With Next i 'When the loop is complete, print the number of paragraph styles in use in the Immediate Window. Debug.Print iP End With End Sub
The comment lines in the code explain each step. Notice that the statement
Debug.Print is used to display the specified information in the Immediate Window. When you use
Debug.Print in a loop of this sort, it automatically starts a new line each time. So, in this case, you'll end up with a nice vertical list of style names followed by the number of styles listed.
Remember that this example is to help give you more information when you're trying to resolve a problem, such as figuring out how many styles you need to deal with to clean up the formatting in your document. If your document returns a large number, that's not necessarily indicative of a problem—it's only information to be used to help you determine the best approach to take.
InUseproperty does not mean that the style is actually applied to content in the document. It may represent a style that is always actively available for use with a built-in feature or a style that has previously been applied in that document (or that was applied in the template that the document is based on).
Another VBA construct that can be particularly handy when troubleshooting documents is the ability to loop through all of the story ranges in a document. For an example of how to do this, see the article Using Office Open XML to Save Time Without Writing Code.
Debunking Common Troubleshooting Myths
When you begin to use VBA to help troubleshoot problem documents, be sure that you enlist VBA to simplify rather than complicate the solution. It's very common for people to expect that document problems require a dramatic resolution when the opposite is usually the case. The answers to the following common troubleshooting myths can help you keep things simple:
Troubleshooting myth #1. Solutions to problem documents are usually complex and time-consuming to resolve.
Because Word is a powerful program, people often expect it to be more difficult and complex than it is. But, it's usually true that the less work you do, the better your document will be. So, when a Word document misbehaves, there is almost always a simple reason.
For example, I was asked to help with a problem that a user believed to be a corrupt Normal template (the default global template in Word). The problem was that a white box with a black outline appeared wherever a picture should be in any document. When first believing the issue to be a corrupt document, the user recreated the document. Then, realizing that any document they opened became affected, they replaced the Normal template. When this failed, they reinstalled Office. However, the issue persisted.
The actual solution was to uncheck the Show Picture Placeholders setting in Word Options.
Troubleshooting myth #2. I have no idea what was wrong with my document, but I fixed it. It's working fine now.
Sorry, but you probably didn't fix it at all. If you don't know what's wrong, you can't effectively fix it. You can become quite skilled at temporarily fooling the document into behaving correctly, but the trouble is almost certain to return.
In fact, something might sound like a problem but have nothing at all to do with the behavior that sent you troubleshooting. For example, the document that I'm working in right now contains 270 styles. Are you shocked and horrified? Am I likely to have problems with this document? Well, what if I told you that just 6 of those are custom styles because a new, clean and healthy Word 2007 document contains 264 built-in styles by default? Those 264 styles are not all in use or visible by default but they are available if you need them. What's more, they are supposed to be there and pose no danger whatsoever.
Remember that you need to know why something is a problem before you can determine how to fix it.
Troubleshooting myth #3. It will be faster to just recreate the document from scratch or strip it to plain text and reformat it.
Please don't do it! If the document is much longer than one page, redoing all of that work is unlikely to be faster than finding and fixing the trouble. In fact, in more than ten years of troubleshooting documents professionally, I have never run across a single long document that was better off being retyped from scratch. And, I have seen exactly one that was better off being stripped and reformatted (not surprisingly, that document had no corruption in it whatsoever, just several years' worth of overcomplicated formatting).
Expanding Your Troubleshooting Options
If there is a moral to the story in this article, it's to remember that VBA can be enormously useful, especially when used as an extension of the program functionality that you already know. That is, go to VBA when it can help you obtain information that you can't easily get from within the UI, or when using VBA will save time or simplify a task. For this purpose, VBA is simply another feature of Microsoft Office, just like all of the features that you knew before you first opened the VBE.
Remember that the most effective approach to troubleshooting a document is always to use the simplest solution available.
In addition to VBA, the Open XML Formats can make it easier to fix and troubleshoot some types of problems with 2007 Office release documents. With Open XML Formats, virtually every bit of content and formatting is accessible to you under the hood of your documents. To get started learning about Office Open XML, see the additional resources that follow.
About the Author
Stephanie Krieger is a Microsoft Office System MVP and the author of two books, Advanced Microsoft Office Documents 2007 Edition Inside Out and Microsoft Office Document Designer. As a professional document consultant, Stephanie helps many global companies develop enterprise solutions for Microsoft Office on both platforms. She also frequently writes, presents, and creates content for Microsoft. You can reach Stephanie through her blog, arouet.net.