Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, and 2013. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Understanding Functions.
by Allen Wyatt
(last updated February 15, 2014)
You already know that you can use subroutines in your macros. VBA also 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
There are two macros in this code. One is the function (TestFunc) and the other is a macro (Macro1) that is used to invoke the function. When the function is executed, it can do anything that is done in a regular macro. In this case, it performs a test that results in the TestFunc variable being set to either True or False. Note that this variable name is the same as the function name. This is the value that is returned by the function to whatever program called it.
Which brings us to the Macro1 macro. Note that the TestFunc macro can appear on the right side of the equal sign. This makes functions very powerful and an important part of any program. VBA executes the function and returns whatever value is appropriate from that function and assigns it to the variable on the left side of the equal sign (TooMany). The program then acts upon the value returned.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12961) applies to Microsoft Word 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Word here: Understanding 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!
When developing a macro, you may need to swap the values in two variables. It's simple to do using the technique in this tip.Discover More
Do you need to do some simple math using dates in a your macro? One of the easy functions you can use is the DateDiff ...Discover More
Do you want to easily jump to the top of a page in your document? You can use the Go To command to make the shift, or you ...Discover More
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.