Microsoft KB Archive/99000

{|
 * width="100%"|

WD: Sharing Variables Between User-Defined Subroutines

 * }

Q99000

-

The information in this article applies to:


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

-

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 &quot;global&quot; (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

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

Sub SumRoutine Print a; &quot; +&quot; ; b;&quot; is&quot;;a + b  End Sub The above macro displays &quot;2 + 3 is 5&quot; 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 &quot;0 + 0 is 0.&quot; Without declaring the A and B variables as shared, the subroutine called &quot;SumRoutine&quot; does not have access to the values of A and B.

The following macro displays &quot;2 + 3 is 5&quot; 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 ; &quot; +&quot; ; b ; &quot; is&quot; ; 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:

"Q94374 Passing Parameters &quot;By Reference&quot; and &quot;By Value&quot;" "Q94734 CALL Statement Cannot Accept Variable Argument"