WD: Sharing Variables Between User-Defined Subroutines

ID: Q99000

The information in this article applies to:

SUMMARY

If you want to share the contents of a variable between subroutines, you must declare (dimension) the variable as a global variable.

MORE INFORMATION

To define a variable as "global" (to make it accessible in multiple subroutines), use the Dim command with the shared parameter. The following sample macro defines variables A and B as global variables.

NOTE: The Dim statement for global variables must appear before the

Sub MAIN statement.

   Dim Shared A, B
   Sub MAIN
      A = 2
      B = 3
      Call SumRoutine
   End Sub

   Sub SumRoutine
      Print a; " +" ; b;" is";a + b
   End Sub

The above macro displays "2 + 3 is 5" on the status bar at the bottom of the screen. If you omit the first line of the macro (the Dim statement), the result is "0 + 0 is 0." Without declaring the A and B variables as shared, the subroutine called "SumRoutine" does not have access to the values of A and B.

The following macro displays "2 + 3 is 5" on the status bar without defining the A and B variables as global variables.

   Sub MAIN
      A = 2
      B = 3
      Call SumRoutine a, b
   End Sub

   Sub SumRoutine(a, b)
      Print a ; " +" ; b ; " is" ; a + b
   End Sub

Each variable in the comma-delimited parameter list must correspond to a value that the subroutine being called is prepared to receive.

NOTE: The WordBasic language for Word versions 1.x for Windows allows you to call and pass parameters to routines within the current macro only. It is not possible to call or pass values to subroutines within other macros in versions 1.x of Word for Windows.

For more information on passing parameters to another macro, please see the following articles in the Microsoft Knowledge Base:

   ARTICLE-ID: Q94374
   TITLE     : Passing Parameters "By Reference" and "By Value"

   ARTICLE-ID: Q94734
   TITLE     : CALL Statement Cannot Accept Variable Argument

REFERENCES

"Using WordBASIC for Word for Windows and Word for OS/2," by WexTech Systems Incorporated, pages 122-123

"Using WordBasic," by WexTech Systems and Microsoft, pages 58-59, 157

Additional query words: subroutine passing parameter value share sub dim global

Keywords          : kbmacro kbusage kbmacroexample winword word6 winword2 word7 word95 
Version           : 1.x 2.x 6.0 6.0a 6.0c 7.0 7.
Platform          : WINDOWS
Issue type        : kbinfo

Last Reviewed: February 6, 1998