Using Macros and Keyboard Shortcuts to Automate Tasks in Word

Using Macros and Keyboard Shortcuts to Automate Tasks in Word
Page content

Introduction

The use of mail merge as described in the previous article allows us to reduce the number of repetitious tasks in assembling our pages. It also reduces the chances of errors and inconsistencies. One issue it doesn’t deal with is the separation of the merged document into separate web pages. This is in breach of my principle of constructive laziness: “Never do yourself what you can get the computer do for you.”

The Macro Facility in Word

Macros are collections of instructions which are stored in a small program. This can be executed by a keystroke or attached to a menu or toolbar. For those of us allergic to Visual Basic, the easiest way to assemble a macro is to record a sequence of instructions. From experience, the most straightforward, reliable and unambiguous way to do this is through a sequence of keystrokes. Using the mouse can sometimes have unforeseen consequences.

Planning your macro

A macro is like a computer program. It’s always worth thinking in advance about the steps that you need to carry out to achieve your desired purpose.

The steps we wish to automate to separate out our web pages are fairly straightforward.

1. Identify a section of our continuous document that corresponds to a single web page.

2. Select this section

3. Cut it from the continuous document, leaving it in the clipboard.

4. Delete the page break to set up the next section, so that the process can be repeated.

In order to achieve this, we need to know how to achieve each of these steps.

1. Each web page finishes with the tag. We will find this tag, and then move to the end of the line to ensure we don’t leave the tag behind.

2. From there, we select the text back from there to the top of the page

3. We use the Cut option, which places the selected text in the clipboard, and deletes it from our continuous merged document.

4. In order to delete the page break, we simply delete the relevant characters until the first line of the next page is visible at the top.

The key strokes required for this process are as follows:

1. Press [Ctrl] and F, Type , Press [Enter], Press [End]

2. Press [Ctrl] and [Shift] and [Home]

3. Press [Ctrl] and F

4. Press [Del] twice

Recording your macro

To record your macro:

Go to Tools > Macro > Record New Macro.

Select (Assign to) Keyboard

Now press the combination of keys you wish to use to recall this recorded macro. I used [Ctrl] and [Shift] and X when I tried this.

Once you have pressed the key combination, select Assign with the mouse, and you are ready to record your macro. I used the sequence listed above.

Once complete, use the mouse to select the Stop button. Using the keys for commands to be recorded and the mouse to control the recording process prevents ambiguities.

Your completed macro is actually a Visual Basic application which looks like this:

Sub Macro1()

'

’ Macro1 Macro

’ Macro recorded 22/10/2008 by UCLan User

'

Selection.Find.ClearFormatting

With Selection.Find

.Text = “”

.Replacement.Text = “ ”

.Forward = True

.Wrap = wdFindContinue

.Format = False

.MatchCase = False

.MatchWholeWord = False

.MatchWildcards = False

.MatchSoundsLike = False

.MatchAllWordForms = False

End With

Selection.Find.Execute

Selection.EndKey Unit:=wdLine

Selection.HomeKey Unit:=wdStory, Extend:=wdExtend

Selection.Cut

Selection.Delete Unit:=wdCharacter, Count:=1

Selection.Delete Unit:=wdCharacter, Count:=1

End Sub

Having entered the command once, and pasted the cut page into notepad to be saved as a web page, you should be at the right place in your Word document to enter your key sequence to run the macro and repeat the process with a single keystroke.

Conclusions

Macros are another way of applying my principle of constructive laziness. Is it worth it? If you are generating 5 pages, maybe not. However, by the time you are generating 16 pages, it will have saved you over 100 keystrokes. Even if you don’t want to make web pages this way, you can apply this approach to other repetitive tasks with Word.

Any downsides? Well, the only problem I encountered was that it happened very quickly, and the next page looks very like the previous one, so remember to paste your selection before moving on or you will lose it.