You are currently offline, waiting for your internet to reconnect

WD: Calling One Macro Routine or Function from Another Macro

This article was previously published under Q81414
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
In Microsoft Word for Windows, you can call macro subroutines and functionsthat are defined in one macro from within another macro. You can use thistechnique to create libraries of common routines and avoid having to copyor rewrite procedures you use often.


For more information about calling macro subroutines and functions, whilein the Visual Basic for Applications Editor click the Office Assistant,type "Call," click Search, and then click to view "Call Statement."

All other versions of Word listed above


To call a macro subroutine from within another macro, use the followingsyntax:
MacroName is the name of the macro containing the routine, and RoutineNameis the name of the routine you want to use. The template containing thelibrary routine, or a document to which the template is attached, must beopen when the routine is called. Routines stored in the NORMAL.DOT templateare always available. For example, if you create a macro called Lib1, whichcontains the subroutine MyBeep, you can call MyBeep from other macros. Thefollowing 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 ina macro called SwitchToDotPath:
   Sub MAIN      MsgBox "This is a test"      Lib1.MyBeep   End Sub				

The syntax for using a function that you defined in another macro issimilar to that used for calling a subroutine. For example, if you define afunction called MyDateTime$(time$) in the Lib1 macro library, you couldcall that function from another macro, titled CheckDateTime. The followingis the MyDateTime$(time$) function:
   Function MyDateTime$(time$)      mDate$ = Date$()      mTime$ = Time$()      MyDateTime$ = mDate$ + " " + mTime$   End Function				
You can call the MyDateTime$(time$) function from the following macro,called CheckDateTime:
   Sub Main                          'CheckDateTime macro      CheckDateTime$ = "91.09.30 11:50 AM"      ThisDateTime = Lib1.MyDateTime$(ThisDateTime$)      Print ThisDateTime$      If ThisDateTime$ = CheckDateTime$ Then         Lib1.MyBeep         MsgBox "It's time for lunch!"      Else         MsgBox "It's not time for lunch yet!"      EndIf   End Sub				
"Using WordBasic," by WexTech Systems and Microsoft, pages 58-59
1.x 1.0 1.1 1.1a 2.0 7.0 word95 winword macword 6.0.1 word7 word6 winword2 6.0 word8 word97 8.0

Article ID: 81414 - Last Review: 11/16/2006 05:03:09 - Revision: 2.3

  • Microsoft Word 2.0 Standard Edition
  • Microsoft Word 2.0a
  • Microsoft Word for Windows 2.0b
  • 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
  • kbmacro KB81414