Word.Tips.Net Welcome toWord.Tips.Net

Helpful Links

Tips.Net Home
WordTips Home

Ask a Word Question
Make a Comment

Tips.Net Store

WordTips FAQ
WordTips Premium

Learn Access Now
Free Printable Forms

Beauty Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Legal Tips
Money Tips
Organizing Tips
Pest Tips
Pet Tips
Wedding Tips
Word2007 Tips
WordTips

Advertise on the
WordTips Site

Newest Tips

Setting Fraction Bar Overhang Spacing in the Equation Editor

Printing On Both Sides of the Paper

Turning Off AutoComplete for Dates

Ordering Search and Replace

Understanding Auto Line Spacing

Adding Comments to Your Document

Conditional Calculations in Word

 

Understanding WordBasic Subroutines

Summary: Using subroutines, you can divide your WordBasic programs into logical chunks you can easily manage. (This tip works with Microsoft Word 6, and Word 95.)

When you write macros, you are using a programming language called WordBasic. This language is based on the BASIC programming language, with extensions specific to Word. One of the features of the language is the capability to use subroutines in your programs. For instance, consider the following macro:

Sub MAIN
    TestSub
End Sub

Sub TestSub
    Print "In the macro"
End Sub

This simple macro does nothing but call a subroutine (TestSub), which in turn prints a message on the status bar. The subroutine then returns control to the main program. You can have as many subroutines in a WordBasic program as you desire. The purpose of each should be to perform common tasks so you don't have to rewrite the same code all the time.

You can also pass parameters to your subroutines. These parameters can then be acted upon by your subroutine. For instance, consider the following macro:

Sub MAIN
    A = 1
    PrintIt A
End Sub

Sub PrintIt(x)
    Print x
End Sub

This is a simple macro that sets a variable, and then passes it in a subroutine call to PrintIt. This subroutine displays the value of the variable on the status bar, and then returns to the calling program. Notice that the subroutine does not use the same variable name as it was passed. This is because WordBasic reassigns the value of x (what the subroutine expects to receive) so that it matches the value of A (what the program is passing to the subroutine). The important thing to remember in passing parameters to subroutines is that your program must pass the same number of parameters as the subroutine expects, and that the parameters must be of matching types and in the proper order.

Tip #137 applies to Microsoft Word versions: 6 | 95

Take Control! Master the real power behind Word! Successfully master the secrets of powerful formatting and create documents that stand out from the rest. Best of all, you can create documents that are easy to maintain and quick to change.
 
Check out Word 2007 Styles and Templates today!