by Jack Lyon
Alas, poor Yorick! I knew him, Horatio, a fellow of infinite jest, of most excellent fancy. He hath borne me on his back a thousand times, and now, how abhorred in my imagination it is! My gorge rises at it. . . . Now get you to my lady’s chamber and tell her, let her paint an inch thick, to this favor she must come. Make her laugh at that.
Hamlet, act 5, scene 1.
Corruption is never funny, especially when you’re on deadline and the Word document you’ve been editing starts doing not-so-funny things, such as:
- Repeatedly renumbering pages.
- Repeatedly rebreaking pages.
- Showing incorrect layout and formatting.
- Showing strange or unreadable characters.
- Producing error messages.
- Omitting text that should be there.
- Displaying text that shouldn’t be there.
- Hanging your computer.
All of these are symptoms of document corruption, which is most often caused by one or more of the following:
- Tracked changes in documents moved from PC to Macintosh or vice versa.
- Master documents.
- Nested tables.
- Automatic list numbering.
- Automatically updated document styles.
- Fields, especially cross-references.
- Deleted note reference numbers (in the notes themselves, not in the main text).
- Saving when resources are low.
- A corrupt printer driver (Word often crashes when printing).
- A corrupt document template, especially Normal.dotm.
Early word processors, such as WordPerfect, kept track of text and formatting as a clean, continuous string of characters and codes that looked like this:
When creating Word, Microsoft took a different approach, using numeric pointers to specify what was going on in a document. For example, characters 7 through 15 of paragraph 10 might be given the attribute of italic. (That’s not technically exact; I’m just trying to convey the general idea here.) The problem is, those pointers can — and sometimes do — get out of whack and end up pointing at the wrong thing. A typical Word document has thousands of these pointers, which are stored in paragraph breaks and section breaks. Pointers for the document as a whole are stored in its final paragraph break, and this is often where corruption occurs. The usual solution is to “maggie” the document (a process named for Margaret Secara from the TECHWR-L mailing list). Here’s how:
- Back up your document, so if something goes wrong, you’ll have something to go back to.
- Select all of the text in the document.
- Hold down SHIFT and press the left-arrow key to deselect the final paragraph mark.
- Copy the selected text.
- Create a new document.
- Paste the text into the new document.
- Use the new document rather than the old one.
That, however, may not be enough. If your document has section breaks, they too can hold corruption, which means you’ll need to maggie each section separately—selecting its text, deselecting the section break at the end, and copying and pasting the text into a new document, adding new section breaks as needed. If you have lots of sections, this will take lots of time.
A better solution might be to use a macro that will automatically maggie each section (including the final paragraph mark). Here is such a macro that I hope you’ll find useful when corruption strikes. Note that when using the macro, you should have open only one document — the one you want to maggie.
Sub AutoMaggie() Dim s As Integer Dim secType As Integer Dim secCount As Integer Dim myDoc As Document Set myDoc = Documents(ActiveDocument.FullName) Documents.Add DocumentType:=wdNewBlankDocument Documents(myDoc).Activate secCount = ActiveDocument.Sections.Count For s = 1 To secCount - 1 secType = ActiveDocument.Sections(s + 1).PageSetup.SectionStart ActiveDocument.Sections(s).Range.Select Selection.MoveEnd Unit:=wdCharacter, Count:=-1 Selection.Copy ActiveWindow.Next.Activate Selection.Paste Selection.InsertBreak Type:=wdSectionBreakContinuous ActiveDocument.Sections(s + 1).PageSetup.SectionStart = secType myDoc.Activate Next s 'Copy and paste final section ActiveDocument.Sections(secCount).Range.Select Selection.MoveEnd Unit:=wdCharacter, Count:=-1 Selection.Copy ActiveWindow.Next.Activate Selection.Paste End Sub
Now let’s look at the macro to see how it works.
Sub AutoMaggie()
That first line simply gives your macro (technically a subroutine, or “Sub”) a name, which can be anything you like.
Dim s As Integer Dim secType As Integer Dim secCount As Integer Dim myDoc As Document
Those four lines set up (dimension) four arbitrarily named variables, which simply hold information. (If you remember high school algebra, you were always trying to solve for the variable X.) Here, s, secType, and secCount are defined as integers (whole numbers); myDoc is defined as a document.
Set myDoc = Documents(ActiveDocument.FullName)
That line assigns the name of your soon-to-be-maggied document to the variable myDoc.
Documents.Add DocumentType:=wdNewBlankDocument Documents(myDoc).Activate
Those two lines create a new blank document and switch back to your original document (whose name is stored in myDoc).
secCount = ActiveDocument.Sections.Count
Here, we count the number of sections in the active document (our original) and assign the number to the variable secCount.
Now the fun starts:
For s = 1 To secCount - 1
That tells word to do whatever comes next a certain number of times, starting with the number 1 and ending with one less than the number of sections in our document. Why one less? You’ll see in a minute.
secType = ActiveDocument.Sections(s + 1).PageSetup.SectionStart
Here we get the type (continuous, next, odd, or even) of the next section (“s + 1”) and store that information in the variable secType.
ActiveDocument.Sections(s).Range.Select Selection.MoveEnd Unit:=wdCharacter, Count:=-1 Selection.Copy
In those three lines, we select the text of the section specified by s: If s is 1, we select the first section; if 2, the second section; and so on. After selecting the section, we move the cursor one character to left so that we’re not selecting the section break (which could hold corruption). Finally, we copy our selection.
ActiveWindow.Next.Activate Selection.Paste
Here, we switch to our new, clean document and paste the text that we copied from the section in our original document.
Selection.InsertBreak Type:=wdSectionBreakContinuous ActiveDocument.Sections(s + 1).PageSetup.SectionStart = secType
Next, we insert a continuous section break. Oddly, the first line alone won’t do what we need, even if we specify the Type using the variable secType. We must add the second line, which needs an existing section break in order to work. It turns that break into whatever type we stored earlier in the variable secType. (It took a lot of experimenting to pin down this wretched fact.) Again, we have to add 1 to the section number (“s + 1”) because we’re inserting the breaks after the section text.
myDoc.Activate
Here, we switch back to our original document.
Next s
This is the line that has been incrementing the s variable that we specified earlier in the line “For s = 1 To secCount – 1”.
ActiveDocument.Sections(secCount).Range.Select Selection.MoveEnd Unit:=wdCharacter, Count:=-1 Selection.Copy ActiveWindow.Next.Activate Selection.Paste
We select the last section in our original document, move back one character, copy the text, move to our new document, and paste.
End Sub
Finally, we end the macro.
After running the macro, you’ll be left with your original document (unchanged) and a new, unnamed document that is identical to your original but without the corruption.
That’s it! There are ways to make this macro more elegant and efficient, but, to paraphrase Blaise Pascal, I would have written a shorter macro, but I did not have the time. Nevertheless, the macro works for me; the next time you encounter corruption, I hope it works for you.
________
How to Add a Macro to Word & to the QAT
Here’s how to put this macro (or any other) into Microsoft Word so it will be available when you need it:
- Copy the text of the macro, starting with the first “Sub” and ending with the last “Sub.”
- Click the “View” tab on Microsoft Word’s ribbon.
- Click the “Macros” button.
- Type a name for the macro in the “Macro name” box — probably the name used after the first “Sub.” For this macro, that’s “AutoMaggie.”
- Click the “Create” button.
- Delete the “Sub AutoMaggie” and “End Sub” lines that Word created in the macro window. The macro window should now be completely empty (unless you already have other macros in there).
- Paste the macro text at the current insertion point.
- Click “File,” then “Close and Return to Microsoft Word.”
To actually use the macro:
- Place your cursor at the beginning of the document.
- Click the “View” tab on Microsoft Word’s ribbon.
- Click the “Macros” button.
- Click the name of your macro to select it.
- Click the “Run” button. (If you wanted to delete the macro, you could press the “Delete” button instead.)
Here’s how to put the macro on Word’s QAT (Quick Access Toolbar):
- Locate the QAT (it’s probably on the top left of your screen either above or below Word’s Ribbon interface).
- Right-click the QAT.
- Click “Customize Quick Access Toolbar.”
- Under “Choose commands from:” click the dropdown list and select “Macros.”
- Find and select your macro in the list on the left.
- Click the “Add” button to add it to the QAT.
- Click the “OK” button to finish.
Jack Lyon (editor@editorium.com) owns and operates the Editorium, which provides macros and information to help editors and publishers do mundane tasks quickly and efficiently. He is the author of Microsoft Word for Publishing Professionals, Wildcard Cookbook for Microsoft Word, and of Macro Cookbook for Microsoft Word. Both books will help you learn more about macros and how to use them.
You must be logged in to post a comment.