An American Editor

November 12, 2014

Lyonizing Word: Formatting with Macros

Formatting with Macros

by Jack Lyon

Most users of Microsoft Word format text by selecting a paragraph and then applying a font. More advanced users apply a style. Here’s why:

  1. Styles are easier to use than direct formatting. Once you have the style set up (with, say, 12-point Arial bold, condensed by 1 point, left justified, with 24 points of leading above and 12 points of leading below), you can apply that style with a single click. But if you apply the same formatting without using a style, you’ll have to make a dozen clicks for each heading. If your document has 50 headings, that’s hundreds of clicks—versus 50 clicks if you use a style.
  2. If you need to change the formatting of, say, level-2 headings, you can simply modify the style rather than tediously selecting each heading and applying a different font. Modify the style, and the formatting of all those headings is automatically changed.

But there is a way to handle formatting that is even more powerful—by using macros.

Suppose that you’ve dutifully applied styles to the various parts of a document, but then your client asks you to change the font—everywhere in the document—from Times New Roman to Adobe Garamond. (No, you should not just select the whole document and apply Adobe Garamond. Why? Because that simply “paints over” the real formatting that is still there in the styles, and it will almost certainly lead to inconsistent formatting somewhere down the line.) You could manually modify each style, but if there are dozens of styles in use, there is a better way. That way is a macro, like this one:

Sub SetFontInAllStyles()
Dim aStyle As Style
For Each aStyle In ActiveDocument.Style
aStyle.Font.Name = “Adobe Garamond”
Next
End Sub

Well, that was easy. Let’s look at each line of the macro (excluding the first and last lines, which simply define the beginning and end of the macro).

Dim aStyle As Style

This line defines (dimensions) a variable, aStyle (which name I just made up), as a style. At one point as the macro runs, aStyle might represent the style Heading 1. At another point it might represent Heading 3. But it will always represent a style.

For Each aStyle In ActiveDocument.Styles

Here’s where things start to get interesting. That line tells the macro to cycle through each style (represented by aStyle) in all of the styles in the active document (the document in which your cursor is currently sitting).

aStyle.Font.Name = “Adobe Garamond”

And that line tells Word to set the font for the style currently being represented by aStyle to be Adobe Garamond.

Next

The “Next” line tells Word to go to the next style in the document.

When you run the macro, it will cycle through each style in the document (For Each…) and set Adobe Garamond as the font used in that style.

But what if you want to change the font only in heading styles (Heading 1, Heading 2, and so on)? Try this:

Dim aStyle As Style
For Each aStyle In ActiveDocument.Styles
If InStr(aStyle.NameLocal, “Heading”) Then aStyle.Font.Name = “Adobe Garamond”
Next
End Sub

Here’s the line of interest:

If InStr(aStyle.NameLocal, “Heading”) Then aStyle.Font.Name = “Adobe Garamond”

That line uses a macro command we haven’t seen before, InStr, which checks for specific text in a larger string of text. In this case, it checks to see if the text “Heading” appears in the name of the style (for example, “Heading 4”). If it does, then the macro sets the font for the Heading style as Adobe Garamond.

Armed with that little beauty, you can pull off all kinds of formatting marvels. Here are just a few of the options available:

If InStr(aStyle.NameLocal, “Heading”) Then aStyle.Font.Bold = True

If InStr(aStyle.NameLocal, “Heading”) Then aStyle.ParagraphFormat.Alignment = wdAlignParagraphCenter

If InStr(aStyle.NameLocal, “Heading”) Then aStyle.NoSpaceBetweenParagraphsOfSameStyle = True

You can even specify the exact name of the style (rather than using InStr):

If aStyle.NameLocal = “Normal” Then aStyle.ParagraphFormat.Alignment = wdAlignParagraphJustify

If aStyle.NameLocal = “Heading 3” Then aStyle.Font.Italic = True

All of Word’s formatting options are at your disposal.

So yes, if you’re formatting a Word document, you should always use styles. But if you need to modify styles en masse, now you know how.

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.

________

How to Add 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 “______________.”
  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 ___________________.
  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.

May 15, 2013

The Only Thing We Have to Fear: Wildcard Macros

Whenever I talk to colleagues about macros, it is as if a funereal pall has enclosed us. My colleagues, generally, tell me that they cannot write macros, that it is much too complicated, especially wildcard macros.

If I ask if they ever use Word’s Find & Replace, they all admit that, yes, they do. “Congratulations,” I say, “because each time you use Find & Replace, you have written a macro! You just haven’t recorded it.”

The only thing we have to fear about macros is our fear of macros.

I suppose, technically, Find & Replace is not macro writing, but truly, a macro is just a way to find some sequence and do something to that sequence — be it bold the sequence, highlight it, replace it with another sequence, delete it, whatever.

Most everyone who uses Microsoft Word has recorded a keyboard macro. Word makes doing so very easy. Again, congratulations if you have written a keyboard macro, because you are on your way to macro wizardship.

There is a key to writing macros. It is a secret that macro wizards rarely share, but I’m going to share with you. The secret is wrapped up in a single magical word: analysis. Analysis of what you need a macro to do is the key to writing a macro. Sure you need to have some arcane language (what good is wizardry without arcane language all its own?) and all of the arcane language you need to write the macros can be found in Jack Lyon’s Macro Cookbook for Microsoft Word and in Wildcards in MS Word Macros, which is a compilation of information on wildcards that Jack Lyon wrote for his blog years ago and which you can download for free by clicking the title-link. Alternatively, you can use the Wildcard Find & Replace Macro found in EditTools to “write” the macros for you, but analysis is the real key to writing macros.

Consider this problem: You have a list of 100 references in which the styles are all over the place. Author names are often listed like this:

Arnold, J. H., K. L. Swift, and A.J.H. Archimedes.

but you need the author names to look like this:

Arnold JH, Swift KL, Archimedes AJH:

You can fix the names manually or by using macros. Manually will take nearly forever, so the better method is to use macros. Here is where analysis matters.

When I began using macros, I saw this problem and thought, “How can I write a macro to fix these author names?” My thinking was a single macro to take care of it all. I quickly discovered that a single macro can’t do the job, but a series of macros that can be combined into a single macro could. The key was series of macros, which meant that I needed to break the problem down into solvable (or macroable) parts.

The first part is Arnold, J. H., which I need to change to Arnold JH,. What I need to find is as follows:

  1. any mix of letters of varying length
  2. that is followed by a comma
  3. and a space
  4. a single uppercase letter
  5. followed by a period and a space
  6. a single uppercase letter
  7. followed by a period
  8. and a comma and a space

I need to replace the find list with

  1. the mix of letters found in 1
  2. the space found in 3
  3. the single uppercase letter found in 4
  4. the single uppercase letter found in 6
  5. and the comma and space found in 8

Note that what I no longer need is not included in the list of replace with items (i.e., find items 2, 5, and 7). Also note that, in analyzing what needs to be found, items that I no longer want are listed on their own lines in the find list.

If you are using Word’s Find & Replace dialog with Use Wildcards checked, you would manually enter the following Find string [paired parens represent the information on a single line in the find list, thus, ([A-z]@) represents line 1: any mix of letters of varying length]:

([A-z]@)(,)( )([A-Z]{1,1})(. )([A-Z]{1,1})(.)(, )

And the following Replace string (the backslash+number represents the corresponding find item, e.g., \1 represents line 1: any mix of letters of varying length and \8 represents line 8: a comma and a space):

\1\3\4\6\8

I can hear you groan. But it isn’t as difficult as it appears. All of the information to write the strings is available in the downloadable Wildcards in MS Word Macros document (just click on the link).

If you are using the EditTools’ Wildcard Find & Replace Macro, you click buttons to make your selection and the code is written for you. An added feature with the Wildcard Find & Replace Macro is that you can save this find and replace so that you can reuse it in the future; with Microsoft’s Find & Replace, the strings cannot be saved. However, what I used to do before I created the Wildcard Find & Replace Macro — and recommend that you do — was keep a special Word document with these strings in it so I could copy and paste when needed in the future. I set up the file like this:

1. Change Arnold, J. H., to Arnold JH,
Find: ([A-z]@)(,)( )([A-Z]{1,1})(. )([A-Z]{1,1})(.)(, )
Replace: \1\3\4\6\8

Once you have entered the strings in either Microsoft’s Find & Replace dialog or in the Wildcard Find & Replace Macro, click Replace All and all author names that fit this particular format will be altered. Then move to the next series to analyze, which is to change K. L. Swift, to Swift KL,. In this instance, what I need to find is as follows:

  1. a single uppercase letter
  2. followed by a period and a space
  3. a single uppercase letter
  4. followed by a period
  5. and a space
  6. any mix of letters of varying length
  7. that is followed by a comma
  8. and a space

I need to replace the find list with:

  1. the mix of letters found in 6
  2. the space found in 8
  3. the single uppercase letter found in 1
  4. the single uppercase letter found in 3
  5. the comma found in 7
  6. and the space found in 5

What I no longer need is not included in the list of replace with items (i.e., find items 2 and 4). Also note that, in analyzing what needs to be found, items that I no longer want are listed on their own lines in the find list.

If you are using Word’s Find & Replace dialog with Use Wildcards checked, you would manually enter the following Find string:

([A-Z]{1,1})(. )([A-Z]{1,1})(.)( )([A-z]@)(,)( )

And the following Replace string:

\6\8\1\3\7\5

I said that you can’t save the strings as a macro if you are using Word’s Find and Replace dialog. That is true as far as it goes, but it doesn’t go all that far. There is a way to save the strings as a true macro without using EditTools’ Wildcard Find & Replace Macro. What you do is record a simple Find and Replace macro, for example, find bush and replace it with blues, using Word’s Record Macro feature, and give it a name like WildcardAuthorCorrection1; be sure to keep a list of what that macro does (or will do once you edit it). (If you don’t know how to record a simple macro, the fastest and best way to learn is to use Jack Lyon’s Macro Cookbook for Microsoft Word. Within a few minutes you will be a master at recording simple macros and even at editing them.)

Open the newly recorded macro to edit it, and replace the .Text = bush entry with .Text = [your find string] and replace the .Replacement.Text = blues with .Replacement.Text = [your replace string]. Make sure all the items labeled as True are changed to False except change .MatchWildcards = False to .MatchWildcards = True.

Once you get hooked on macros, the possibilities are endless and you’ll never let go. More importantly, you will improve your editing speed, accuracy, and efficiency, which translates into a higher effective hourly rate and a more profitable editing business.

You’ve got nothing to fear — macros are conquerable!

Blog at WordPress.com.

%d bloggers like this: