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

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

Advertise on the
WordTips Site

Newest Tips

Underlining Quoted Text

Changing Tabs Using the Ruler

Moving Drawing Objects

Standardizing Note Reference Placement

Selecting Printing of Color Pictures

Stubborn Foreign Languages

Sizing the Preview Pane

 

Understanding WordBasic Functions

Summary: When you use functions, you define program routines that return values you can use elsewhere in your program. (This tip works with Microsoft Word 6, and Word 95.)

You already know that you can use subroutines in your macros. WordBasic allows you to define functions that can be used in your macros. The difference between functions and subroutines is that functions can return values, whereas subroutines cannot. Consider the following macro:

Sub MAIN
    TooMany = TestFunc
    If TooMany Then Print "Too many pages"
End Sub

Function TestFunc
    TestFunc = 0
    If SelInfo(4) > 10 Then TestFunc = -1
End Function

This function returns either the value 0 (False) or -1 (True), depending on a test it performs. The main program then acts upon the value returned. Notice that the function name can appear on the right side of an equal sign. This makes functions very powerful and an important part of any program. Within the function the result is assigned to TestFunc, which is the name of the function itself; this is the value returned by the function.

As with subroutines, you can also pass parameters to your functions. This is illustrated in the following macro:

Sub MAIN
    A = 12.3456
    Print A, Round(A)
End Sub

Function Round(X)
    Round = Int(X + 0.5)
End Function

This simple macro defines a number, and then prints it and the result of passing the number to the Round function; the output is 12.3456 and 12. Notice that the parameter should be passed to the function within parentheses. Also notice that the function does not use the same variable name as it was passed. This is because WordBasic reassigns the value of x (what the function needs) so it matches the value of A (what the program is passing to the function). The important thing to remember in passing parameters to functions is that your program must pass the same number of parameters as the function expects, and the parameters must be of matching types and in the proper order.

Tip #139 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!