Microsoft KB Archive/99958

{|
 * width="100%"|

WD: Macro to Test for an Existing Macro Name

 * }

Q99958

-

The information in this article applies to:


 * Microsoft Word for Windows, versions 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
 * Microsoft Word for Windows, versions 7.0, 7.0a

-

SUMMARY
When you write macros in Word, it may be necessary to test for the existence of a macro before running the macro with the WordBasic ToolsMacro statement. If your macro does not check for the existence of a macro, you may receive one of the following error messages:

There is no such macro.

MORE INFORMATION
WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this macro code &quot;as is&quot; without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

The following WordBasic macro uses the IsExecuteOnly function to test for an existing macro named &quot;Test&quot; in the global template file, Normal.dot:

  Sub Main On Error Goto errors MacroName$ = &quot;Test&quot; exe = IsExecuteOnly(&quot;Global:&quot; + MacroName$) ToolsMacro .Name = MacroName$, .Run     'Run global macro if it exists errors: If err = 511 Then MsgBox Chr$(34) + MacroName$ + Chr$(34) + &quot; macro does not exist&quot; End Sub To test for the existence of a template-based macro, change the word &quot;Global:&quot; to the name of the template in the above macro. For example:

  exe = IsExecuteOnly(&quot;Memo2:&quot; + MacroName$)