Macro Cookbook Sample
Macro Cookbook Sample
•
•
Understanding macros
Looping macros
Macro
Cookbook
• Using macros that make decisions
• Using “found” macros
• Adapting “found” macros
• Using common macro commands
• Using secret macro commands
for Microsoft Word
Lyon
• Storing macros for safety and sharing
The Editorium
ISBN 978-1-4341-0332-1
90000>
testing sweet, sweet success!
Jack Lyon
The Editorium
9 781434 103321
Macro Cookbook
for Microsoft Word
Jack Lyon
T HE E DITORIUM
Please read this: The reader (that’s you) assumes the entire risk as
to the use of this book and the information therein. I don’t anticipate
any problems, but all computers and Microsoft Word installations are
different, and I can’t be held responsible for what might happen with
yours. To avoid problems, be sure to back up your files before trying
anything in this book; then you’ll have something to go back to if
anything goes wrong. As the lawyers say, this book is provided for
informational purposes only and without a warranty of any kind, either
express or implied, including but not limited to implied warranties of
merchantability, fitness for a particular purpose, and freedom from
infringement.
ISBN 978-1-4341-0332-1
The Editorium
3907 Marsha Drive
West Valley City, UT 84128
www.editorium.com
[email protected]
Contents v
Preface vii
Introduction ix
1 Understanding Macros 1
Recording a Macro . . . . . . . . . . . . . . . . . . . . . . . . 5
Looking at the Macro . . . . . . . . . . . . . . . . . . . . . . . 7
Running the Macro . . . . . . . . . . . . . . . . . . . . . . . . 8
Word Happens . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2 Looping Macros 11
Do and Repeat . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Other Looping Commands . . . . . . . . . . . . . . . . . . . . 15
3 Decisions, Decisions 19
“Returning” Information . . . . . . . . . . . . . . . . . . . . . 21
The Powers-Wilson Doctrine . . . . . . . . . . . . . . . . . . . 25
v
vi
6 Basic “Ingredients” 50
Moving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
Selecting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Moving and Selecting . . . . . . . . . . . . . . . . . . . . . . . 52
Inserting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Deleting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Using Bookmarks . . . . . . . . . . . . . . . . . . . . . . . . . 53
Using Revisions . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Displaying Messages . . . . . . . . . . . . . . . . . . . . . . . 54
Finding and Replacing . . . . . . . . . . . . . . . . . . . . . . 55
Running Macros . . . . . . . . . . . . . . . . . . . . . . . . . 58
7 Secret “Ingredients” 59
Looking Under the Hood . . . . . . . . . . . . . . . . . . . . . 60
Overriding a Word Command . . . . . . . . . . . . . . . . . . 61
Word Happens—Again . . . . . . . . . . . . . . . . . . . . . . 63
Listing Word’s Commands . . . . . . . . . . . . . . . . . . . . 65
8 Storing Macros 66
Creating Your Own Template . . . . . . . . . . . . . . . . . . 67
Adding a Template Automatically . . . . . . . . . . . . . . . . 69
Making Macros Accessible . . . . . . . . . . . . . . . . . . . . 70
Index 89
Preface
www.editorium.com/cookbookcommands.zip
http://support.microsoft.com/kb/187243
http://support.microsoft.com/kb/211800
vii
viii Preface
1. To change options on a Macintosh, click the “Word” menu and then “Prefer-
ences.”
Introduction
ix
x Introduction
3. This assumes, of course, that you’re charging by the job, the page, or the
word (which you should be) rather than by the hour. If you charge by the hour,
you’re limiting the amount of money you can make, because no matter how
efficient you are, you have only so many hours to sell. If, on the other hand, you
charge by the job, the page, or the word, your hourly income grows every time
you figure out a way to work more efficiently. That’s what macros are all about.
Introduction xi
further than that, you can learn to write your own macros
from scratch—in other words, learn to program. In this
book, however, I just want to show you how to use macros
without learning to program. Is that really possible? Let’s
find out.
Chapter 1
Understanding Macros
Ingredients
• 2 eggs
• 1 cup buttermilk
• 1 cup vegetable oil
1
2 Understanding Macros
Directions
Selection.Font.Bold = True
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "supercede"
.Replacement.Text = "supersede"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
1. The lowline _ character at the end of the first line (and elsewhere in this
book) just breaks up the macro command if it gets too long for easy reading. You
can delete it and merge the two lines if you want to.
4 Understanding Macros
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
And so it does!
Understanding Macros 5
R ECORDING A M ACRO
So there’s a correspondence between Word’s features
and Word macros—not too surprising. The question is,
how can you find out what the macro is for any particular
feature? The answer is, you record a macro while using the
feature. Try this in Microsoft Word—no, seriously, try it:
2. Click the little arrow under the “Macros” button (on the
right).
4. Give your macro a name (“Test” will do) and click the “OK”
button.
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "supercede"
.Replacement.Text = "supersede"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
8 Understanding Macros
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
The macro will play, reproducing the steps you took manu-
ally while you were recording it. And for many tasks, that
may be all you need.
Any time you have repetitious tasks to perform, record
those tasks and then play them back. Let the computer do
the work—that’s what it’s for.
Index
accessibility of macros, 70
adding macros to Microsoft Word, 28–29
add-in templates
activating, 69
adding, automatically, 69–70
creating, 67–69
all caps, fixing, in body text, 47–49
Beverley, Paul, 27
bookmark commands, 53
checking commands, 54
commands
accessing list of, 65
bookmark commands, 53
checking commands, 54
deleting commands, 52–53
find commands, 55–56
in hidden Word features, 60–61
inserting commands, 52
message box commands, 54–55
moving and selecting commands, 51–52
moving commands, 50–51
overriding, 61–64
replace commands, 57–58
revision commands, 53–54
run macro commands, 58
selection commands, 51–52
complex macros, recording, 77–88
cursor, commands for, 50–51
89
90 Index
decision-making macros
overview of, 19–21
and Powers-Wilson Doctrine, 25
and returned information, 21–25
deleting commands, 52–53
Do-While loop, 15, 21
Electric Editors, 27
“ephemeralization,” 88
hidden features
and listing Word commands, 65
and overriding Word commands, 61–64
viewing, 59–60
viewing underlying commands for, 60–61
inserting commands, 52
looping macros
additional looping commands, 15–18
and decision-making macros, 21
steps for, 11–14
versatility in, 14–15
Index 91
Normal template, 66
Powers, Hilary, 27
Powers-Wilson Doctrine, 25
recorded macros
problems with, 9–10
running, 8, 74
viewing, 7–8
recording macros
looping macros and, 11
and overriding Word commands, 63–64
and Powers-Wilson Doctrine, 25
problems in, 9–10
recording complex macros, 77–88
recording find-and-replace macros, 74–77
steps for, 5–7, 71–74
repeat commands. See looping macros
replace commands, 57–58
returned information, for decision-making macros, 21–25
revision commands, 53–54
running macros, 8, 29, 58
templates
activating, 69
adding, automatically, 69–70
creating, 67–69
Title Case macros, 40–47
92 Index
understanding macros
and adapting macro commands, 33–49
clarifying macro structure and, 31–32
looking at recorded macros, 7–8
overview of, 1–4
recording macros, 5–7
running macros, 8
variables, 24n2
While/Wend loop, 16
With/End With loop, 17
Word features, and recording macros, 5–7, 8. See also hidden features
“Word-VBA Code Samples for Newbies & Others” (Zawawi), 50
Wyatt, Allen, 27, 30
Zawawi, JoJo, 50