Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Writing a Macro from Scratch.

Writing a Macro from Scratch

by Allen Wyatt
(last updated September 8, 2012)

4

Many of the tips used in WordTips rely upon macros in order to run. Some readers may not know how to enter a macro from scratch in Word. There are actually two ways you can create macros. First you can record a macro, which is appropriate when you want to record a series of steps you perform quite often. The second method of creating a macro, writing one from scratch, is much more powerful.

To create a macro from scratch, follow these steps:

  1. Make sure the Developer tab of the ribbon is displayed.
  2. In the Code group, click Macros. Word displays the Macros dialog box. (You can also display the Macros dialog box by simply pressing Alt+F8.)
  3. Using the Macros In drop-down list (near the bottom of the dialog box), select where you want your new macro stored. Select Normal.dotm (Global Template) if you want your macro available in all documents; select a different template if you want the macro available only with that particular document template. You can even assign a macro to a specific document.
  4. In the Macro Name box, type a descriptive name you want assigned to the macro you are writing. (Make sure the name doesn't have any spaces in it.) Optionally, you can enter information in the Description box.
  5. Click on Create. The VBA Editor is started and you can write your macro.
  6. When you are through, close the macro window by selecting the Close and Return to Microsoft Word option from the File menu, or press Alt+Q.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (6821) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Writing a Macro from Scratch.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Files Open in Word 2003, but not Word 2000

If you have some documents that open in Word 2003, but not in Word 2000, it may mean that you are running into some strange ...

Discover More

Removing a Directory

Your macro, in the course of doing some processing, may create a directory that you later need to delete. Here's how to get ...

Discover More

Using a Formula to Replace Spaces with Dashes

If you need a formula to change spaces to some other character, the SUBSTITUTE function fits the bill. Here's how to use it.

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Determining the Number of Fonts Available

When creating a macro, you may need to figure out how many fonts are available to Word. You can do this using the FontNames ...

Discover More

Assigning a Macro to a Shortcut Key

Do you have a macro that you use frequently? Using the file menu to access the macro can be time consuming. This tip talks ...

Discover More

Displaying a Message in the Status Bar

A great place for your macro to display status information is, well, in the status bar. Displaying the information is easy, ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 4 + 1?

2017-08-31 17:20:09

Kim

I'm confused about how to use the macros in these tips. Do we follow these instructions and copy and paste what's in the grey box? I've tried but keep getting errors. What am I missing?


2017-08-06 02:52:06

Ken Endacott

HSK

Your error message is occurring because the statement starts with a period and is not preceded by a With statement. If you are using the SaveAsPDF2 macro most likely you have inserted the sPath = .Parh & “\” statement outside the With grouping.

With … End With is used as follows:
The statements
Selection.Font.Bold = True
Selection.Font.Italic = True
Selection.Font.ColorIndex = wdBlue

are equivalent to:
With Selection.Font
.Bold = True
.Italic = true
.ColorIndex = wdBlue
End With


2017-08-05 13:30:50

HSK

The tip for installing a macro to automatically create a PDF did not work. When I checked using the help button theI was directed to a web site and the comment was;

Invalid or unqualified reference

office 365 dev account|Last Updated: 6/12/2017|1 Contributor
An identifier beginning with a period is valid only within a With block. This error has the following cause and solution:

The identifier begins with a period. Complete the qualification of the identifier or remove the period.

Any advice HSK


2016-03-17 10:02:25

Debbie Click

I have created a macro that unprotects, and unhides text in a document. It works perfectly (finally) on my computer but when I email it or load it in SharePoint the Macro does not work. I have gone into the macro organizer and the macro is in the specific template and the normal template, but still does not work for others. Can you please tell me what I am doing wrong?


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.