An American Editor

January 4, 2012

The Professional Editor: Working Effectively Online VIII — Macros Redux

In The Professional Editor: Working Effectively Online VII –  Macros Again, I discussed how I make use of a decision tree to design macros. Jack Lyon, the master of macros and author of Macro Cookbook for Microsoft Word, approaches macros differently. In today’s guest article, Jack discusses his approach to macros.

__________________

Life in a Macro

by Jack Lyon

After mentioning my new Macro Cookbook over the course of several blog posts, Rich Adin has graciously asked me to write a guest editorial related to macros, something I’m delighted to do. In his most recent post, Rich described his technique for sketching out a flow chart on paper, which helps him outline what he wants a macro to achieve before he starts working on the macro itself. This time-honored technique for programming is clarifying and efficient, especially in the early stages of a macro:

As I read Rich’s post, however, I realized that it’s been many years since I created a flowchart before starting to make a macro. Why is that? I wondered. What’s changed? And what do I do differently now?

I think what’s changed is that I’m now a lot more focused on the outcome of a macro rather than its process. As my programming skills have improved, I’ve become more concerned with what rather than how, with ends rather than means because the ability to create those ends has become almost second nature. And I think most skills are like that.

I’m a moderately skilled jazz musician (Hammond organ with Leslie speaker — oh, yeah), but I still have lots to learn, and when I’m working on a new run, I have to play quite mechanically until finally my fingers learn where I want them to go. After that, I can use the run in a variety of songs. But a run isn’t a song. And it’s the song that’s important. The song is the run’s reason for being.

When I’m creating a macro, the first thing I do is decide precisely what I want the macro to do. Some examples:

  • Title case every paragraph styled as Heading 1.
  • Remove extra spaces between footnotes.
  • Convert an automatically numbered list into a manually numbered list.

After getting the purpose firmly in mind, I usually work on a simple macro to see if what I want to do is even possible (proof of concept). If I were trying to title case every paragraph styled as Heading 1, for example, my thinking might go something like this:

Okay, first I need to create a macro that finds a paragraph styled as Heading 1. Hmmm. Probably easiest just to record it.

So I record it. Here’s what I get:

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles(“Heading 1”)
With Selection.Find
.Text = “”
.Replacement.Text = “”
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

Now, what’s that command to title case selected text? Can’t remember. Probably easiest (again) just to record it.

So I record it. Here’s what I get:

Selection.Range.Case = wdTitleWord

Well, that will take care of one instance. What about all the rest?

Then I realize that this is a classic case of a macro pattern I use all the time:

  1. Find something.
  2. Do something to what was found.
  3. Find the next something.

See my Macro Cookbookfor more on this. Basically, it just means adding the following construction at the end of the macro:

While Selection.Find.Found = True
[Do something here]
Selection.Find.Execute
Wend

In this case, I need to add the command to title-case the selected text, like this:

While Selection.Find.Found = True
Selection.Range.Case = wdTitleWord
Selection.Find.Execute
Wend

 So the completed macro looks like this:

Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles(“Heading 1”)
With Selection.Find
.Text = “”
.Replacement.Text = “”
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
While Selection.Find.Found = True
Selection.Range.Case = wdTitleWord
Selection.Find.Execute
Wend

So gradually I’ll put together the various macro commands that work in sequence to do what I need.

Easy for me to say, right? Okay, okay. If you’re just getting started with macros, using Rich’s flowchart is a better way to go. Decide precisely, step by step, the things you want your macro to do, and list those steps:

  1. Find a paragraph styled as Heading 1.
  2. Title-case the paragraph.
  3. Repeat steps 1 and 2 until no more paragraphs are found.

Then get the commands for each step (by recording, borrowing, or whatever).

Then put the commands together to do what you need.

Finally, test your macro to see if it does what you planned. If it doesn’t, revise it until it does.

As this is the beginning of a new year, I’m feeling a little philosophical, so I’m wondering if we could apply a similar process to life. Can we figure out precisely what we want to have happen and then figure out the essential steps to make it so? Or is life more complex and unpredictable than that? Time to get out those flowcharts!

I hope this new year will be a happy one for you and yours.

__________________

It strikes me that Jack’s approach is to have a single focus and then to combine several single-focus macros into a single macro that runs serially. I may be wrong about his approach, but I do think it demonstrates how different the approaches to writing macros can be. Jack’s approach is also one that an experienced macro creator can use, but I think for those of us who are not at the mastery level, flow-charting is better because it helps us focus on the steps.

What do you think? Which approach will you adopt?

1 Comment »

  1. “What do you think? Which approach will you adopt?”

    I will take the passive approach and buy the book along with whatever prepackaged macros I need to aid my business.

    Like

    Comment by Carolyn — January 4, 2012 @ 6:17 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: