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: Understanding the Select Case Structure.
Written by Allen Wyatt (last updated February 13, 2021)
This tip applies to Word 97, 2000, 2002, and 2003
Macros in Word are written in a language called VBA. Like any other programming language, VBA includes certain programming structures that are used to control how the program executes. One of these structures is the Select Case structure. This structure has the following syntax:
Select Case expression Case expression program statements Case expression program statements Case Else program statements End Select
When a macro is executing and this structure is encountered, Word uses the expression (first line) to test each subsequent Case statement to see if the code under the Case statement should be executed. For instance, consider the following code:
Select Case DayOfWeek Case 1 DayName = "Monday" Case 2 DayName = "Tuesday" Case 3 DayName = "Wednesday" Case 4 DayName = "Thursday" Case 5 DayName = "Friday" Case 6 DayName = "Saturday" Case 7 DayName = "Sunday" Case Else DayName = "Unknown day" End Select
This code assumes you enter it with DayOfWeek already set to a numeric value. Let's say (for example's sake) the value is 4. In this structure, the only code that would be executed is the code under the Case 4 statement—in other words, the macro would set DayName to "Thursday." If DayOfWeek were set to some other value not accounted for by the Case statements (outside of the 1 to 7 range), then the code under Case Else would execute, and the macro would set DayName to "Unknown day."
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 (130) 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: Understanding the Select Case 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!
Need to find out the day of the year for a particular date? It's easy to do if you are using a macro. All you need to do ...
Discover MoreDo 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 MoreMacros can be used to read and write all sorts of files. If those files are on a different drive than the current one, ...
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