Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Passing Parameters to Functions.
Written by Allen Wyatt (last updated August 31, 2024)
This tip applies to Word 97, 2000, 2002, and 2003
When you create your own functions, it is often helpful to pass parameters to the function. These parameters can be used either as data that you want the function to act upon or as settings used to control how the function does its work. How you pass parameters to functions is illustrated in the following macros:
Sub Macro1() A = 12.3456 Status.Bar = A & " " & Round(A) End Sub
Function Round(X) Round = Int(X + 0.5) End Function
This simple macro (Macro1) defines a number and assigns it to the variable A. It then prints that number 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. (The variable A is passed to the function when it is invoked; within the function this value is referred to by the variable name X.) This is because VBA reassigns the value of X (what the function needs) so it matches the value of A (what the program is passing to the function).
VBA allows you to specify multiple parameters to be passed to a function. The parameters simply need to be separated by commas in both the declaration of the function and whenever the function is called. 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.
Note:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1536) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Passing Parameters to Functions.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
The heart of creating powerful programs in VBA is to understand how to create subroutines. These structures allow you to ...
Discover MoreDo some macro programming in VBA and you'll quickly find out that you can use functions to extend the power and ...
Discover MoreAn elegant way to run macros from within macros is to use the Call statement. In order to use it, you need to provide a ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments