WD: Macro to Test for an Existing Macro Name

ID: Q99958

The information in this article applies to:

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 "as is" 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 "Test" in the global template file, Normal.dot:

   Sub Main
      On Error Goto errors
      MacroName$ = "Test"
      exe = IsExecuteOnly("Global:" + MacroName$)
      ToolsMacro .Name = MacroName$, .Run      'Run global macro if it exists
   errors:
      If err = 511 Then
         MsgBox Chr$(34) + MacroName$ + Chr$(34) + " macro does not exist"
   End Sub

To test for the existence of a template-based macro, change the word "Global:" to the name of the template in the above macro. For example:

   exe = IsExecuteOnly("Memo2:" + MacroName$)

REFERENCES

"Using WordBasic" by WexTech Systems, Inc., and Microsoft Corporation, page 246

Kbcategory: kbusage kbmacro KBSubcategory: Additional query words: 2.0 2.0a 2.0a-CD 2.0b 2.0c word6 winword 7.0 word95 word7 winword2 6.0 ToolsMacro run macro exist template global error 511

Keywords          : kbmacro kbmacroexample 
Version           : 2.x 6.0 6.0a 6.0c 7.0 7.0a
Platform          : WINDOWS

Last Reviewed: July 31, 1997