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 the If ... End If Structure.
by Allen Wyatt
(last updated November 29, 2014)
Macros in Word are written in a language called Visual Basic for Applications (VBA). Like any other programming language, VBA include certain programming structures used to control how the program executes. One of these structures is the If ... End If structure. The most common use of this structure has the following syntax:
If condition Then ' program statements Else ' program statements End If
When a macro is executing, and this structure is encountered, Word tests whatever condition you have specified following the If keyword. If the condition is true, then the program statements right after the Then keyword are executed. If they are not true, then the statements after the Else keyword are executed. The Else keyword and any following program statements (which together make up an Else clause) are optional; you do not need to include them in your macro.
Regardless of whether the program statements in the If ... End If structure are executed, when Word is done with the structure, the macro continues running with the statement following the End If keyword.
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (13327) 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 the If ... End If Structure.
Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!
When writing a macro, a common task is the need to compare two strings. You can do this by "normalizing" the strings, as ...Discover More
Want a fast way to add brackets around a selected word? You can use this simple macro to add both brackets in a single step.Discover More
One of the basic programming structures used in VBA is the While ... Wend structure. This structure helps to make the ...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.