Debugging a Macro

Written by Allen Wyatt (last updated March 9, 2019)
This tip applies to Word 97, 2000, 2002, and 2003


1

In Word, macros are written in a language called Visual Basic for Applications, or VBA. When you write a macro, you need to test it and correct any errors in the macro. This process is called debugging. The process of debugging a macro is the same as debugging in any other programming language. All you need to do is step through the macro, one command at a time, and make sure it works as you think it should. You do this by viewing both the windows for your macro and a test document. As you step through the macro (using the commands available in the Debug menu of the VBA Editor), you can correct any errors you locate.

As you are debugging macros, you need to make sure you think through every possible way the macro could be used and all the possible conditions that could exist at the time the macro is invoked. Try the macro out in all these ways and under all these conditions. In this way, you will make your macro much more useful.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (751) applies to Microsoft Word 97, 2000, 2002, and 2003.

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

Adding Excel Information to a Web Page

Besides saving a worksheet as a complete Web page, you can also save smaller portions of your data to an existing Web ...

Discover More

Inserting from a Camera or Scanner

One of the ways you can insert graphics into a worksheet is by grabbing them from a digital camera or a scanner. This tip ...

Discover More

Disabled Page Setup Tools

It can be frustrating if you expect to use some of Excel's tools normally available on the ribbon, but those tools are ...

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 (menu)

Determining an ANSI Value

You may need to determine the numeric value of a character in a macro. You can do that using the Asc function, described ...

Discover More

Repeating In a Macro

Macros are often used to process information stored in documents. Usually the processing involves some sort of iterative ...

Discover More

Determining If a Number is Odd or Even

Need to know if a number in a macro is odd or even? It's easy to figure out with the simple calculation shown in this tip.

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}] (all 7 characters, in the sequence shown) 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 2 + 8?

2021-12-16 20:07:04

Allan

Hi,

I have come across this macro in youtube which you separate the mail merge in individual custom file. However, every time I run the macro it pop-up an
Run-time error '4198': Command failed (debug message) but it would generate the letters and save it individually as it suppose to be.

I can't seem to find the problem with the string. Hope you can help me with this...

singleDoc.SaveAs2 _
FileName:=masterDoc.MailMerge.DataSource.DataFields("DocFolderPath").Value & "\" & _
masterDoc.MailMerge.DataSource.DataFields("DocFileName").Value & ".docx", _
FileFormat:=wdFormatXMLDocument


This Site

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

Videos
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.