PDF Microsoft Word 2013 Macros - University of Reading

IT Training

Unit name goes here

Microsoft Word 2013TM Macros (Level 3)

Contents

Introduction

1

Creating a Macro

2

Running a Macro

4

Editing a Macro

4

A Second Example

5

Word Units

6

A More Complex Example

6

Setting Changing Shortcut Keys

8

Copying Macros to Another Document or Template 8

The DEVELOPER Tab

9

Macro Security Levels

10

Deleting a Macro

11

Introduction

A macro is a series of commands and instructions grouped together which, when run, carry out specific tasks automatically. They are especially useful for repetitive tasks or ones which involve several separate steps. You can use macros to:

? carry out routine editing and formatting ? combine multiple commands ? automate a complex series of tasks

In fact the majority of Word commands themselves are macros - there are over 1500 macros built into the system.

There are two ways to create your own macro in Word. The easier method is to use the Macro Recorder to record the actions you want carried out. Having recorded them once, they can then be repeated over and over again simply by using the macro. The other method is to write the macro in Visual Basic. This is much more difficult and to do it you need to know how to program. You can also record a macro and then use the Visual Basic Editor to alter part of it.

1

Microsoft Word 2013 Macros Macros can be assigned to a Quick Access Toolbar button or shortcut key. You can also press (or use the [Macros] button on the VIEW tab) to display a list of the available macros. You can then select the required macro from the list and run it.

By default, macros are stored in the special Normal.dotm template so that they are available for use with all documents but they can also be stored in a specific template or in a document. Because viruses are often spread through macros, they now have to be stored in special macro-enabled files and templates.

Creating a Macro

In order to write a macro from scratch, using Visual Basic, you need to know how that programming language works. In this document it is assumed that you do not have that expertise and so the Macro Recorder will be used instead. This is in fact the way most macros are created, even by users who do know Visual Basic. The code produced by the Recorder can then be edited to produce exactly what's required. You will be trying this out later in the notes. Let's begin with a relatively simple macro which automatically types some text (e.g. your name or department name) in bold letters and centres it. If you want the macro stored in a particular template then you must either open or create a document based on that template or open the template file itself. If you want the macro available in all documents or just in a particular one, then you open or create the document in the usual way (as described below):

1. Load up Word, as usual, or press to create a new document 2. Move to the VIEW tab, click on the down arrow attached to [Macros] on the far right then

choose Record Macro... The Record Macro dialogue box will appear:

3. Enter a name for the macro - type BoldName Note that the name must consist of a continuous alphanumerical sequence - you cannot use BoldName or Bold Name. By default the macro will be stored in Normal.dotm, a template used to store macros for all your documents. You can, however, store the macro in the current document by selecting this option from the Store macro in: box:

2

Microsoft Word 2013 Macros 4. Click on the list arrow on the right of the Store macro in: box and choose Document1 (document) 5. If you want, type a description for the macro in the Description: box 6. To assign your macro to the keyboard or button, click on the appropriate icon under Assign macro to - here, click on [Keyboard] and the Customize Keyboard dialog box will appear:

7. In the Press new shortcut key: box enter the keystrokes required, e.g. (case is significant)

8. Change Save changes in: to the current document (Document1) 9. Check this isn't already assigned under Currently assigned to: then press for [Assign] 10. Finally, press to [Close] the Customize Keyboard window Everything you now do is being recorded, so take great care to do it properly. Note the mouse pointer in Word has a recording icon attached to it. 11. Carry out the sequence of actions you want to record:

a. Press or click on [Bold] to turn on bold text b. Press or click on the [Center] button to centre the text c. Now type in your text (here, type in your name or that of your department) d. Press or click on [Bold] again to turn off bold text e. Press then press or click on [Align Left] to return to normal typing 12. Click on the [Macros] list arrow and choose Stop Recording to finish recording your macro IMPORTANT: when recording a macro, mouse movements made within the document window are not saved. For example, you cannot use the mouse to move the insertion point, to select text or items, or right click to copy, for example, but you can use it to click on menus and other options. Tips ? Before you begin recording a macro, plan the steps you want the macro to perform - it's a good idea to write them down on paper in the order required ? If you make an error while recording a macro this, together with the corrections, will also be recorded. However, you can always edit the macro later to remove any unwanted steps ? Note the Pause Recording option - you can use this to temporarily stop recording, test out the next sequence of steps then Resume Recorder again ? Some commands display dialog boxes and you may need to answer these inside the macro. This can sometimes be avoided - for example, if you save a document immediately before closing it then you are not asked whether you want to save any changes

3

Microsoft Word 2013 Macros ? If the macro includes the command Advanced Find or Replace, click on [More] button on the Find tab, and make sure All is set in the Search box. This stops Word asking whether you want to continue searching if you reach the end of the document ? If you want to use the macro in other documents make sure it doesn't depend on the current content ? If you use a particular macro often, assign it to a toolbar button or shortcut key. That way, you can run the macro directly without having to open the Macros dialog box

Running a Macro

You can now try out your newly created macro as follows: 1. Press to select your current text and it 2. Now press - your text should appear, centred and in bold letters

Macros which have not been assigned to a keystroke (or toolbar button) must be run using the [Macro] button on the Ribbon. Even macros which have been assigned can be run using this method, as you will see:

3. Click on the [Macros] button (or press ) 4. Make sure All active templates and documents is selected in the Macros in: box 5. In the Macro name box, select the macro you want to run - BoldName 6. Press or click on [Run] - your text should appear a second time Tip: To see the built-in macros, press then select Word commands from the Macros in: dropdown list.

Editing a Macro

You can make changes to a macro you have recorded using the Visual Basic Editor. For example, you may wish to remove unnecessary steps, or add instructions that could not be recorded in Word. Ideally, you need to know how to write programs in Visual Basic to do this. However, follow the instructions below and try to make sense of what's happening:

1. Click on the [Macros] button (or press ) 2. Make sure the Macros in: box is set to All active templates and documents 3. In the Macro name: box select the macro you want to edit (here, BoldName) then click on [Edit] The Editor window now appears. Don't worry too much about how this works; just read through the macro instructions, which hopefully will make some sense.

4

Microsoft Word 2013 Macros 4. Find the line which types out the text and edit it in some way - (e.g. add your middle name) 5. In the line above which ends in ParagraphCenter, change Center to Right 6. Click on the [Close] button (or press ) to close the editor and return to your document (the changes are saved automatically)

Tip: You can switch between your document and the Visual Basic Editor by pressing . Note that you will need to close the Editor window when you have finished editing.

7. End by testing out your macro - press - you should find your new text is displayed, this time aligned on the right

Though the macro appears to work perfectly, it could in fact be improved. You may have noticed the lines which read Bold = wdToggle. By clicking on the [Bold] button, bold wasn't turned on, it was toggled. If you switch bold on before you run the macro, then your text doesn't appear in bold and at the end of the macro you are not returned to plain letters. Try it:

8. Type in some text (to check it appears as normal) then press for a new line 9. Press to turn on [Bold] then use to run the macro and note what happens 10. Type in some more text (it appears in bold!) then press for a new line

To overcome this problem you would need to establish normal text at the very start of your macro. You could have done this when recording the macro (display the Font dialog box by clicking on the group arrow then select a Font Style of Regular). Try this, if you like, or edit the first line of the macro as follows:

11. Press to switch into the Editor 12. Change the FIRST Selection.Font.Bold = wdToggle to Selection.Font.Bold = True 13. Close the Editor () then run your macro again - it should work whether or not bold is

already on

A Second Example

This second example of a macro reverses the order of two words. It could be used, for example, to reverse someone's name from Surname Firstname to the more conventional Firstname Surname order.

1. Type in your name (limit it to two words ? your name and family name) 2. Move to the VIEW tab, click on the down arrow attached to [Macros] then choose Record

Macro... 3. Enter a name for the macro - e.g. NameSwitch 4. Store the macro in the current document by selecting this from Store macro in: 5. This time, assign it to a [Button] ? the Word Options dialog box appears 6. Select Project.NewMacros.NameSwitch then [Add >>] it to the Quick Access Toolbar 7. Next, click on [Modify...] and choose a symbol for the toolbar button then press for

[OK] 8. Finally, click on [OK] ? your button should be added to the Quick Access Toolbar

Next, record the macro:

9. Carry out the following sequence of actions: a. Press to select the second part of your name b. Now press to [Cut] it to the Clipboard c. Press to move the insertion point before the first part of your name d. Now press to [Paste] it from the Clipboard e. Finally, press to move the insertion point to the end of the line

5

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download