
Tips.Net > WordTips Home > Creating Macros > Subroutines and Functions > Understanding Functions
Summary: Functions are a programming structure that allows you to calculate and return a result. They are extremely powerful, and are therefore commonly used in programming. This tip explains what they are, why you should use them, and how to create them. (This tip works with Microsoft Word 97, Word 2000, Word 2002, and Word 2003.)
You already know that you can use subroutines in your macros. VBA 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 VBA macros:
Sub Macro1()
TooMany = TestFunc
If TooMany Then StatusBar = "Too many pages"
End Sub
Function TestFunc()
TestFunc = False
If Selection.Inforamtion(wdNumberOfPagesInDocument) > 10 Then
TestFunc = True
End If
End Function
This function returns either the value True or False, 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.
Tip #1535 applies to Microsoft Word versions: 97 2000 2002 2003
Step Up and Take Control! Subscribers to WordTips know just how valuable a resource it is. WordTips Premium provides twice the number of exceptional, easy-to-understand tips every week in an ad-free newsletter, as well as substantial discounts on WordTips archives and e-books.
Check out WordTips Premium today!
Learn to use styles and you discover the real power behind Word. Fast, easy, consistent document formatting and updating is within your grasp. (more information...)
Ask a Word Question
Make a Comment
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips