WD: Sharing Variables Between User-Defined Subroutines

Last reviewed: February 4, 1998
Article ID: Q99000
The information in this article applies to:
  • Microsoft Word for Windows 95, 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 "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 : kbmacroexample winword winword2 word6 word7 word95 kbmacro kbusage
Version : 1.x 2.x 6.0 6.0a 6.0c 7.0 7.
Platform : WINDOWS
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 4, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.