Written by Allen Wyatt (last updated January 15, 2025)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and 2021
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. Remember 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. Remember that the TestFunc function 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.
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 (12961) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and 2021. You can find a version of this tip for the older menu interface of Word here: Understanding Functions.
The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2019. Spend more time working and less time trying to figure it all out! Check out Word 2019 For Dummies today!
Your macro may need to determine if the user has overtype mode turned on. You can find out the overtype status easily by ...
Discover MoreSometimes you need to spell-out numbers in a document. When you have spelled-out numbers, at some point you might want to ...
Discover MorePart of developing macros is learning how to use and manipulate variables. This tip examines a technique you can use to ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2021-05-01 08:54:22
Simao Campos
Please note the typo: Selection.Inforamtion -> Selection.Information
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.
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 © 2025 Sharon Parq Associates, Inc.
Comments