You are on page 1of 7

5 Managing Macros and Forms 137

Practice Tasks
The practice files for these tasks are located in the practice files folder for Microsoft
Word 2010. You can save the results of these exercises in the same folder. Change
the file name so that you dont overwrite the sample files. When you are done, try
performing the following tasks:
Open the document MacroExamples.docx. Use the Save As command to save
the document so that it is enabled for macros.

Look over the numbered list at the start of the document, which is formatted
using the built-in Normal style. Record a macro that does the following:

Applies the style Numbered List

Inserts a tab character before the number

Searches for the next period (.)

Repositions the cursor one character to the right, and then inserts a tab
character

Repositions the cursor at the start of the next paragraph

Record a macro that switches the document to Outline view and displays the
first three outline levels. Assign this macro to the keyboard.

5.2 Apply and Manipulate Macro Options


Word provides a number of options for how to run a macro, including built-in macros
you can use if you want to run a macro when you open or close a document. In the following sections, youll learn how to use the AutoOpen macro, how to assign a macro to a
command button, and how you can add a button that runs a macro to the Quick Access
Toolbar.

Running a Macro When You Open a Document


You can define a macro that runs automatically when you open a document or when
you create a document thats based on a template. To do this, you need to name the
macro AutoOpen (in the first case) or AutoNew, and you must save the macro in a
macro-enabled document, using the .docm (for documents) or .dotm (for templates) file
name extension.
For example, you might want to display a document in a particular view and at a certain
zoom percentage each time you open it. In the Record Macro dialog box, name the

138 Exam 77-887 Microsoft Word 2010 Expert

macro AutoOpen, and then switch to the view and zoom settings you want to use as you
record the macro.
You can test your macro by making a small edit to the document (for example, type your
name or something similar). Now switch to a different view, change the zoom setting,
and then save and close the document. Open it again, and youll see that the document
is restored to the settings you specified in the macro.
In addition to AutoOpen, you can use the following Auto macros in Word:
AutoExec This macro runs when you start Word, before Word opens any document. You should save the AutoExec macro in the default template (Normal.dotm).

AutoNew The AutoNew macro runs when you create a new document based on
the template in which the macro is defined. You might use the AutoNew macro
to update fields in the document or to add information such as the documents
creation date to the header or footer.

AutoClose The AutoClose macro runs when you close a document. This macro
should be saved in the current template.

AutoExit The AutoExit macro runs when you quit Word. Save the AutoExit macro
in the default Normal.dotm template.

To run a macro when a document opens

1. Open the document in which you want to define the macro.


2. In the Code group on the Developer tab, click Record Macro.
3. In the Record Macro dialog box, name the macro AutoOpen.
4. Click OK, and follow the steps you want to record for the macro.
5. Click Stop Recording when you finish, and then save and close the document.
6. Open the document to test the macro you recorded.

Running a Macro from a Command Button


In the section Create Forms later in this chapter, you will learn about the types of form
controls you can use in Word 2010. One of the controls you can add to a document is
a command button (a type of ActiveX control), which is included in the group of legacy
controls you can find in the Controls group on the Developer tab. You can assign a
macro to a command button that runs when you click the button.
For the most part, ActiveX controls need to be programmed to carry out their function.
However, if you want to assign a macro to a command button and are unfamiliar with

5 Managing Macros and Forms 139

VBA, record the macro first. You can then refer to the macro by its name when you set
up the command button.
See Also For information about creating a macro, see Using the Macro Recorder earlier
in this chapter.

To display the legacy tools in the Controls group, click the icon in the bottom-right
corner. Click the command button icon, and Word adds a button to the document.
Right-click the button, and then click View Code, which opens the Visual Basic editor
with a click event procedure set up for the button.
You can assign a macro
to a command button
in the Visual Basic
editor. Record a macro
first, and then enter the
name of the macro in
the buttons click event.

To associate a macro with the command button, type the macros name between Private
Sub CommandButton1_Click() and End Sub. Use this approach if you want to assign a
macro you recorded to the command button. Notice in the previous screen shot that the
names of two macros appear (UpdateSalesNumber and PrintReport). A command button
is a convenient way to run more than one macro with a single click. When you set up the
command button, type the name of each macro you want to run in the order you want
to run them.
You can also copy code from a macro you recorded (or wrote) and paste the code
between these lines. Of course, you can write the macro code yourself if you know
enough about VBA.
To assign a macro to a command button

1. On the Developer tab, in the Controls group, open the menu of legacy commands
and then click the icon for the command button.

2. Right-click the command button, and then click View Code.

140 Exam 77-887 Microsoft Word 2010 Expert

3. In the Visual Basic editor, type the name of a macro or add the code you want to
run when you click the button.

4. Close the Visual Basic editor, and then test the button.
More About VBA and the Visual Basic Editor
The information in this chapter is only the start of what you can do with macros,
especially if you learn the essentials of VBA and start to write your own macros or
edit and modify the macros you record. Recording a macro and then opening it
in the Visual Basic editor can teach you a lot about what the VBA code does. For
example, the following code is for a macro that helps format the numbered lists in
the Word documents used to write the chapters of this book.
Sub NumListStyle()
'
' NumListStyle Macro
'
'
Selection.Style = ActiveDocument.Styles("Num List")
Selection.TypeText Text:=vbTab
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.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeText Text:=vbTab
End Sub

In essence, this macro starts by applying the Num List style to the paragraph. It
then inserts a tab character (Selection.TypeText Text:=vbTab). Inside the indented
lines that begin With Selection.Find, the macro searches for the next period (.).
When the macro locates the period (which follows the number for the item in the
list), it moves the cursor one character to the right and then inserts another tab
character. This macro is a simple one, but it saves several trips to the ribbon or to
the keyboard.

5 Managing Macros and Forms 141

To view a macro you have recorded, click Macros in the Code group on the
Developer tab in Word, select the macro in the Macros dialog box, and then click
Edit. The Visual Basic editor provides a number of tools to help you start learning about VBA. When you write lines of code, the Visual Basic editor provides
choices applicable to the type of Word object you are working with. For example,
when you type ActiveDocument (an object shown in the sample macro) followed
by a period, Word displays choices such as AcceptAllRevisions, AddToFavorites,
ApplyTheme, DeleteAllComments, GoTo, Save, ShowGrammaticalErrors, and many
others. If you select ApplyTheme from the list that appears for ActiveDocument
and type an opening parenthesis, the Visual Basic editor indicates that to apply a
theme, you need to add the themes name.
Click Help, Microsoft Visual Basic For Applications Help in the Visual Basic editor
to open an extensive help system with concepts, how-to examples, and a full
reference.

Adding a Custom Macro Button to the Quick Access Toolbar


One of the most convenient ways to run a macro is to add a button for that macro to
the Quick Access Toolbar. You can assign the macro to a button when you first record it
(see Using the Macro Recorder earlier in this chapter) or customize the Quick Access
Toolbar later by using the Word Options dialog box.

142 Exam 77-887 Microsoft Word 2010 Expert

On the Customize The Quick Access Toolbar page, open the Choose Commands From
list and select Macros. In the list of macros, select the one you want to add to the Quick
Access Toolbar and then click Add. In the Customize Quick Access Toolbar list, select
For All Documents if you want the button to appear on the Quick Access Toolbar for all
documents you work on. You can also select the current document if you want to use the
macro within that scope.
To rename the button or to change the icon associated with it, select it in the list of
commands on the Quick Access Toolbar and then click Modify. Use the Modify Button
dialog box to change the display name and to choose an icon. The display name appears
in a ScreenTip when you point to the button with the mouse.
Use a friendly display name
for macro buttons you add
to the Quick Access Toolbar.

To add a custom macro button to the Quick Access Toolbar

1. Click the arrow at the right end of the Quick Access Toolbar, and then click More
Commands.

2. In the Choose commands from list, choose Macros.


3. In the list of macros, select the macro you want to place on the Quick Access
Toolbar, and then click Add.

4. Click Modify, and then select the icon you want to use and change the display
name for the button.

Practice Tasks
The practice files for these tasks are located in the practice files folder for Microsoft
Word 2010. You can save the results of these exercises in the same folder. Change
the file name so that you dont overwrite the sample files. When you are done, try
performing the following tasks:
Open the document RunMacros.docm.

Use the Run button in the Macros dialog box to run the macro named
InsertBoilerplate.

5 Managing Macros and Forms 143

Add a command button to the top-left corner of the document. Open the
Visual Basic editor, and then assign the macros ApplyStyle and InsertDate to
run when the command button is clicked.

Add a button to the Quick Access Toolbar for the macro named CleanUp.

As the last steps, click the command button to run its macros, and then run
CleanUp from the Quick Access Toolbar.

5.3 Create Forms


Forms are more structured than many documents you create in Word. Forms are
designed to collect specific information in particular formats and are often submitted
to other people for processing or approval. Expense reports, invoices, order forms, and
registration forms are among the types of forms you might create in Word.

Designing a Form
Although you can create a form for a single document, most forms are saved as a Word
template so that they can be used more than once. Start with a blank template for a
form you plan to design from scratch, or use one of the form templates available from
Office.com as a starting point.
See Also For more information about document templates, see Apply a Template to a
Document in Chapter 1 in this section of the book.

To keep content controls aligned in a form, you can use a table, although this is not a
requirement. Click Design Mode in the Controls group on the Developer tab before you
start adding controls to your form. Design mode lets you see the tags that identify the
content controls and lets you arrange and edit the content controls you add to the form.
Heres a quick rundown on the controls you can add to a form. Word displays a
ScreenTip that identifies each control:
Rich text Use this control for text fields in which you need to format text as bold
or italic, for example, or if you need to include multiple paragraphs and add other
content such as images and tables.

You might also like