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 November 10, 2017)

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

Page Ranges in a TOC

It is easy to generate a table of contents for a document, and that TOC can contain page number references for each ...

Discover More

Offering Options in a Macro

It is often helpful to get user input within a macro. Here's a quick way to present some options and get the user's response.

Discover More

Determining the Current Page Number

While your macro is processing the text in your document, you may need a way to determine the current page number where ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Moving to the Start or End of the Real Document

The main body of your text is only one part of what makes up the entire document. Documents can consist of other ...

Discover More

Determining an Integer Value

One of the math functions you can use in your macros is the Int function. It provides a way for you to derive an integer ...

Discover More

Controlling Repagination in Macros

Want to turn off document repagination while your macro does its work? Here are two approaches you can use.

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 six more than 8?

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.