Microsoft KB Archive/81414

= WD: Calling One Macro Routine or Function from Another Macro =

Article ID: 81414

Article Last Modified on 11/16/2006

-

APPLIES TO


 * Microsoft Word 2.0 Standard Edition
 * Microsoft Word 2.0a
 * Microsoft Office Word 2003
 * Microsoft Word 2.0c
 * Microsoft Word 6.0 Standard Edition
 * Microsoft Word 6.0a
 * Microsoft Word 6.0c
 * Microsoft Word 95 Standard Edition
 * Microsoft Word 95a
 * Microsoft Word 97 Standard Edition
 * Microsoft Word 6.0 for Macintosh
 * Microsoft Word 6.01 for Macintosh
 * Microsoft Word 6.01 for Macintosh

-



This article was previously published under Q81414



SUMMARY
In Microsoft Word for Windows, you can call macro subroutines and functions that are defined in one macro from within another macro. You can use this technique to create libraries of common routines and avoid having to copy or rewrite procedures you use often.



Word97
For more information about calling macro subroutines and functions, while in the Visual Basic for Applications Editor click the Office Assistant, type &quot;Call,&quot; click Search, and then click to view &quot;Call Statement.&quot;

All other versions of Word listed above
Subroutine:

To call a macro subroutine from within another macro, use the following syntax:

MacroName.RoutineName

MacroName is the name of the macro containing the routine, and RoutineName is the name of the routine you want to use. The template containing the library routine, or a document to which the template is attached, must be open when the routine is called. Routines stored in the NORMAL.DOT template are always available. For example, if you create a macro called Lib1, which contains the subroutine MyBeep, you can call MyBeep from other macros. The following is the subroutine MyBeep: Sub MyBeep Beep : Beep : Beep For t = 1 to 100 : Next         'Pause Beep : Beep : Beep End Sub The following example illustrates how you can use the MyBeep subroutine in a macro called SwitchToDotPath: Sub MAIN MsgBox &quot;This is a test&quot; Lib1.MyBeep End Sub Function:

The syntax for using a function that you defined in another macro is similar to that used for calling a subroutine. For example, if you define a function called MyDateTime$(time$) in the Lib1 macro library, you could call that function from another macro, titled CheckDateTime. The following is the MyDateTime$(time$) function: Function MyDateTime$(time$) mDate$ = Date$ mTime$ = Time$ MyDateTime$ = mDate$ + &quot; &quot; + mTime$ End Function You can call the MyDateTime$(time$) function from the following macro, called CheckDateTime: Sub Main                         'CheckDateTime macro CheckDateTime$ = &quot;91.09.30 11:50 AM&quot; ThisDateTime = Lib1.MyDateTime$(ThisDateTime$) Print ThisDateTime$ If ThisDateTime$ = CheckDateTime$ Then Lib1.MyBeep MsgBox &quot;It's time for lunch!&quot; Else MsgBox &quot;It's not time for lunch yet!&quot; EndIf End Sub

