3651H – Use Advanced Features of Computer Applications

Week 8 – Word Macros

What is a Macro?

A macro is a set of stored keystrokes used to automate a repetitive task or to make a complex operation easier. A macro may simply contain text, or may contain a combination of text and graphics, commands and/or actions.

When you record a macro, all keystrokes and actions are recorded sequentially in the macro. When you run or use the macro, these keystrokes and actions replay in the order they were recorded. Macros are attached to a document template.

Some of the simple tasks and procedures you can easily automate with a macro include the following:

·  Opening, selecting and updating a document filled with field codes.

·  Adding custom zoom or edit buttons to the toolbar.

·  Creating a shortcut key to toggle table grid lines on or off.

·  Opening and arranging collections of related files.

·  Applying or removing styles or heading levels.

·  Opening a document and immediately moving to the last location edited.

·  Storing different display and work settings so you can switch between them easily.

·  Reformatting the active document to meet pre-recorded layout and print settings.

Recording a Word Macro

There are two ways to create a macro:

  1. Use the Macro recorder available under the Tools menu
  2. Write macro code using Visual Basic for Applications(VBA)

The macro recorder records everything you do, including your mistakes until you turn the recorder off so plan what you want to do before you start recording. Once the macro has been recorded the recorder can not be used to go back and edit mistakes. You need to start again or invoke the Visual Basic Editor and modify the VBA code.

Naming a Macro

Each macro must be given a unique name. Macro names cannot have spaces nor special characters like +,-,*,/,@,.,! .

Activity 1 – Create a Macro

  1. Open a new blank document in Word.
  2. Insert about 10 lines of text (anything will do).
  3. Start the macro recorder – Tools, Macro, Record New Macro …
  4. Type DraftCopy for the macro name
  5. In the description type ‘Double space entire document’
  6. Click OK (The macro recorder is now running)
  7. Press CTRL A to select the whole document.
  8. Set the left indent at 1cm and the right indent at 12cm.
  9. Set the font to Arial and size to 12 point.
  10. Press CTRL 2 to set the line spacing to double.
  11. Click the Stop button on the macro toolbar.

Activity 2 – Run Your Macro

  1. Open any word document.
  2. Select Tools, Macro, Macros (or ALT F8)
  3. Highlight DraftCopy
  4. Click on Run.
  5. Look at the result.
  6. Close the file without saving your changes.

Macros are saved in the current template file and are available when working with any file based on that template. Any macros saved in the Normal template (Normal.dot) are global macros that can be accessed by all documents. Macros saved in the current template are local and can be accessed by any file based on that template.

Editing Macros

When you record a macro the steps performed are automatically converted to VBA code and saved. You can amend this code by going into the Visual Basic environment. The general structure of a macro is

Sub macroname()

… (VBA code)

End Sub

The lines that begin with a single quote and are in green are called comments. Comments document the code. The macro recorder adds a comment – macro recorded on date by author at the beginning of every macro.

VBA is an object-oriented language and involves

·  Objects – a thing eg a document or a selection from a document

·  Methods – a method performs a task related to an object eg TypeText, WholeStory, MoveUp

·  Properties – objects have certain properties eg font is a property of a document and font can have sub-properties eg name– Selection.Font.Name = “Arial”, size – selection.Font.Size = 12

In general the code has a structure Object.method or Object.property = value. Note properties use an = sign and methods use :=

Eg Selection.Font.Underline = wdUnderlineSingle

Selection.TypeText Text:=”This text will appear on the screen”

You can cut code from one macro and paste it into another. For example you can combine the actions of two or more macros into the one macro.

Activity 3 – Editing a Macro

  1. Select Tools, Macro, Macros …
  2. Select DraftCopy for the macro name
  3. Click Edit
  4. Find the code window. (Use View, Code if it is not visible).
  5. Change the font to Times New Roman
  6. Change the font size to 14 points
  7. Close the VBA window
  8. Rerun the Macro

Input Boxes inMacros

Often you want to enter information into a document when you run a macro. This can be achieved by adding an input box to your macro. The statement

Selection.TypeText Text:=InputBox(“Please enter customer name”, “Details”)

will produce an input box with the caption Details,the prompt “Please enter customer name”, and two buttons: OK and Cancel on the screen. The InputBox statement is an example of a Visual Basic function.

Activity 4 – Using an InputBox

  1. Open a new blank document.
  2. Type in the following:

Toni’s Teddies

Date: <insert current date>

Dear <insert bookmark called customer>

The <insert bookmark called product> you have ordered is not currently in stock. We have placed an order with our supplier and it should be available in <insert bookmark called numberofdays> days.

We will phone you as soon as it is available.

With regards

Toni

Use Insert, Date and Time … to include today’s date. Use Insert, Bookmark … to insert the bookmarks.

  1. Save this as Out of Stock.dot - a template file.
  2. Open a new word document based on the Out of Stock.dot template called Richard.doc
  3. Create the following macro, called GetDetails, using the VB editor

Sub GetDetails

‘ Macro called GetDetails for Richard’s details

Selection.GoTo What:=wdGoToBookmark, Name:=”customer”

Selection.TypeText Text:=”Richard”

Selection.GoTo What:=wdGoToBookmark, Name:=”product”

Selection.TypeText Text:=”Brown 30cm Teddy”

Selection.GoTo What:=wdGoToBookmark, Name:=”numberofdays”

Selection.TypeText Text:=”seven”

End Sub

6.  Close the VBA editor

7.  Run the macro – what did it do?

8.  You should see that a more general macro would be far more useful. Edit the macro to look like the following:

Sub GetDetails

‘ Macro called GetDetails to add details to out of stock letters

Selection.GoTo What:=wdGoToBookmark, Name:=”customer”

Selection.TypeText Text:=InputBox(”Customer name”, “Details”)

Selection.GoTo What:=wdGoToBookmark, Name:=”product”

Selection.TypeText Text:=InputBox(”Which product”,”Details”)

Selection.GoTo What:=wdGoToBookmark, Name:=”numberofdays”

Selection.TypeText Text:=InputBox(“How many days”, “Details”)

End Sub

  1. Try running this macro. You should notice that you now need to enter the information in when you run the macro. This means you can use this macro over and over and enter the appropriate information in each time you run it.

Automatic Macros

There are two types of automatic macros in Word:

·  AutoNew macros run whenever a document is created. Each template file can store an AutoNew macro

·  AutoExec is a macro that runs whenever Word is started. The AutoExec macro is stored in the Normal template.

Testing a Recorded Macro

After a macro has been recorded, it should be tested. It is recommended, however, that the document be saved prior to executing the macro for the first time, in case it does not perform the required task.

Having saved the macro, you will need to create an appropriately initialised environment (similar, if not identical, to the original environment in which the macro was recorded) to conduct a test. For example, if a macro is designed to manipulate a pre-selected block of text, then ensure that a selection has been made prior to running the macro.

In summary, the steps to implementing and testing a macro are as follows:

  1. Design the macro by making a list of the tasks it is required to perform.
  2. Determine and set any pre-conditions that are necessary prior to recording (and later running) the macro.
  3. Record the macro and save the macro to disk.
  4. Reset any pre-conditions that are necessary prior to running the macro.
  5. Use the checklist to verify tasks are performed correctly.

Task 6 - Word Macros

Part 1:

Using the macro recorder create a macro called Part1, that ensures the following environment is set whenever you run it:

·  View is set to Web Layout.

·  Zoom is set to 70%.

·  Recently Used File List set to 6 entries.

·  Automatic document save every 8 minutes.

·  Font is set to Bookman Old Style, 14 point, line spacing 1.5 lines

·  Page footer to contain your name, page number and file name.

  1. Edit the macro to include your name as a comment.
  2. Save the macro, Part1
  3. Run and test the macro

Part 2:

Design and create a macro that complies with the following specifications:

·  At the current cursor location, the macro inserts the following text in Arial 13 point:

Yours faithfully,

John Smith

per

·  Immediately following the space after per, and on the same line, the macro displays an appropriately captioned text box for user input, into which the operator can type their name to complete the letter.

  1. Create the macro, naming it SignOff, and assign it to the ALT + ‘S’ key combination.
  2. Edit the macro to include your name as a comment.
  3. Save the macro in the NORMAL.DOT template.
  4. Run and test the macro.

3651H - Use Advanced Features of Computer Applications - Week 8 Page 4