An American Editor

September 18, 2017

Lyonizing Word: Corruption

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:

WordPerfect Reveal Codes

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:

  1. Back up your document, so if something goes wrong, you’ll have something to go back to.
  2. Select all of the text in the document.
  3. Hold down SHIFT and press the left-arrow key to deselect the final paragraph mark.
  4. Copy the selected text.
  5. Create a new document.
  6. Paste the text into the new document.
  7. 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:

  1. Copy the text of the macro, starting with the first “Sub” and ending with the last “Sub.”
  2. Click the “View” tab on Microsoft Word’s ribbon.
  3. Click the “Macros” button.
  4. 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.”
  5. Click the “Create” button.
  6. 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).
  7. Paste the macro text at the current insertion point.
  8. Click “File,” then “Close and Return to Microsoft Word.”

To actually use the macro:

  1. Place your cursor at the beginning of the document.
  2. Click the “View” tab on Microsoft Word’s ribbon.
  3. Click the “Macros” button.
  4. Click the name of your macro to select it.
  5. 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):

  1. Locate the QAT (it’s probably on the top left of your screen either above or below Word’s Ribbon interface).
  2. Right-click the QAT.
  3. Click “Customize Quick Access Toolbar.”
  4. Under “Choose commands from:” click the dropdown list and select “Macros.”
  5. Find and select your macro in the list on the left.
  6. Click the “Add” button to add it to the QAT.
  7. 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.

March 3, 2014

Lyonizing Word: The Next Character Macro

Today’s column by Jack Lyon marks the first essay in a new monthly series, “Lyonizing Word.” In this series, Jack will discuss using Microsoft Word, especially macros, to best advantage during the editing process. Please welcome Jack as a new columnist for An American Editor.

______________

Lyonizing Word: The Next Character Macro

by Jack Lyon

Macros and mastering Microsoft Word are keys to success in the business of editing. One can be a great editor and not master either, but it is more difficult, if not near-impossible, to have a successful editing business if you aren’t master of the tools you use.

My plan is to help you master Word and Word macros. My hope is that you will learn from each of my columns and will take the lessons learned and build on them yourself — for example, by building more complex and more useful macros that fulfill a need in your editing business. Here’s my first installment, which I hope you’ll find useful.

The NextCharacter Macro

I often use character codes while finding and replacing in Microsoft Word. What’s a character code? Here are some common ones:

^09 is a tab
^13 is a carriage return
^32 is a space

Especially in a wildcard search, you may have to use such codes because Word’s wildcard search engine can’t handle certain characters and will give you an error message if you try to use them.

But what if you don’t know the code for the character you need? You can probably look it up online or in a computer book, but wouldn’t it be nice to have a macro that would tell you immediately? Just put your cursor in front of the character and run the macro to get the code number. Here’s a macro that will do just that:

Sub NextCharacter()
Dim NextChar As String
NextChar = Str(AscW(Selection))
MsgBox “The code for the next character is” & NextChar
End Sub

That’s pretty simple, but there’s still a lot going on. Let’s look at each line in the macro.

Sub NextCharacter()

Here we tell Word the name of the macro, which is a subroutine (Sub) named NextCharacter. You can use pretty much any name you like, as long as it doesn’t start with a number or include any spaces. And the parentheses at the end of the name? I’ll reserve that discussion for a future article.

Dim NextChar As String

I just made up the name NextChar but not the commands around it, all of which are part of VBA (Visual Basic for Applications), Microsoft’s programming language for Word and other programs in the Office suite. If you want to learn more about “Dim,” “As,” “String,” and other VBA commands, here’s a good place to start: Getting Started with VBA in Word 2010.

In this line, we’re “dimensioning” (Dim) a variable to hold a value (NextChar) that we’ll use later in the macro. A variable is just a placeholder that can hold a value, like X in your high-school algebra class. Dimensioning just tells Word what *kind* of value we’re using — in this case, a string of characters rather than, say an integer, which can only hold numbers.

NextChar = Str(AscW(Selection))

This assigns a value to the NextChar variable. What value? The one produced by this:

Str(AscW(Selection))

“Selection” is the character to the right of your cursor (in the Western world).

“AscW” tells Word to find the ASCII or Unicode value of that character.

“Str” turns that value into a string — that is, characters rather than a value. Why? So we can see those characters in the message box produced by the next line:

MsgBox “The code for the next character is” & NextChar

This displays a message box (MsgBox) that gives us the code for the next character (as stored in NextChar), preceded by the text “The code for the next character is” just to pretty things up.

End Sub

This line simply ends the macro, or subroutine.

Now, here’s how to put this macro (or any other) into Microsoft Word so it will be available when you need it:

  1. Copy the text of the macro, starting with the first “Sub” and ending with the last “Sub.”
  2. Click the “View” tab on Microsoft Word’s ribbon.
  3. Click the “Macros” button.
  4. Type a name for the macro in the “Macro name” box — probably the name used after the first “Sub.” For this macro, that’s “NextCharacter.”
  5. Click the “Create” button.
  6. Delete the “Sub [macro name]” 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).
  7. Paste the macro text at the current insertion point.
  8. Click “File,” then “Close and Return to Microsoft Word.”

To actually use the macro:

  1. Place your cursor directly in front of the character you want to identify.
  2. Click the “View” tab on Microsoft Word’s ribbon.
  3. Click the “Macros” button.
  4. Click the name of your macro to select it.
  5. Click the “Run” button. (If you wanted to delete the macro, you could press the “Delete” button instead.)

After the macro runs, a dialog box will appear with the numeric code for the character. To dismiss the dialog box, click OK.

Now that you know the code for the character after your cursor, you can use that code in Word’s Find dialog. Or you can insert it into your document. To do so, hold down the ALT key and enter the code on the numeric keypad. For example, the code for an uppercase A is 65. You could insert that character by holding down the ALT key and entering 65 on the numeric keypad. Of course, it’s a lot easier just to hold down SHIFT and hit the A key, but what if you need to enter something more esoteric? Microsoft provides a code chart here: ANSI Character Codes Chart.

That’s it! I hope you find the macro useful, and that my explanation here helps you understand a little more about how macros work.

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 and of Macro Cookbook for Microsoft Word. Both books will help you learn more about macros and how to use them.

Blog at WordPress.com.

%d bloggers like this: