Written by Allen Wyatt (last updated June 18, 2018)
This tip applies to Word 97, 2000, 2002, and 2003
If you are just starting out developing macros, you may be looking for a simple way to offer a set of choices to a user, and then take an action based on the user's response. This is a relatively simple task, if you use the InputBox function, along with a Select Case structure.
The first task is to set up your InputBox so it displays the information to the user. For example, let's say you have five options, and you want the user to select one option from those five. You can use the following code to put together five options, each on their own line:
sPrompt = "1. This is your first choice" & vbCrLf sPrompt = sPrompt & "2. This is your second choice" & vbCrLf sPrompt = sPrompt & "3. This is your third choice" & vbCrLf sPrompt = sPrompt & "4. This is your fourth choice" & vbCrLf sPrompt = sPrompt & "5. This is your fifth choice"
You can now use the sPrompt string when you invoke the InputBox function in your macro. You then translate what the user responds with into a number that represents their choice from your five options. The code to do this is as follows:
sUserResp = InputBox(sPrompt, "The Big Question") iUR = Val(sUserResp)
In this example, the response from the InputBox function is assigned to the sUserResp variable, which should be a string. The iUR variable, which is a numeric variable (integer), is then set based on the value of the string. (The Val function returns the value in a string.)
The only thing left to do is to take an action based on which number was chosen, 1 through 5. You can use the Select Case structure to do this. The full subroutine could appear as follows in VBA:
Sub TestInput() Dim sPrompt As String Dim sUserResp As String Dim iUR As Integer sPrompt = "1. This is your first choice" & vbCrLf sPrompt = sPrompt & "2. This is your second choice" & vbCrLf sPrompt = sPrompt & "3. This is your third choice" & vbCrLf sPrompt = sPrompt & "4. This is your fourth choice" & vbCrLf sPrompt = sPrompt & "5. This is your fifth choice" iUR = 0 While iUR < 1 Or iUR > 5 sUserResp = InputBox(sPrompt, "The Big Question") iUR = Val(sUserResp) Wend Select Case iUR Case 1 'Do stuff for choice 1 here Case 2 'Do stuff for choice 2 here Case 3 'Do stuff for choice 3 here Case 4 'Do stuff for choice 4 here Case 5 'Do stuff for choice 5 here End Select End Sub
Notice that this example uses a While ... Wend loop around the InputBox function. This is done to make sure that the user enters a number between 1 and 5. If the value entered is outside that range, then the user is simply asked again.
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 (1366) 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: Offering Options in a Macro.
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 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!
One of the common things done in macros is to somehow "process" documents, which often means moving the insertion point ...
Discover MoreNeed to format your document using a macro? You can easily set the right margin for an individual paragraph by using the ...
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."
2022-05-23 10:35:14
Brother Jeremy, CSJW
I believe the code to cancel should be inserted after
iUR = Val(sUserResp)
If StrPtr(sUserResp) = 0 Then
Exit Sub
End If
thus reading
While iUR < 1 Or iUR > 5
sUserResp = InputBox(sPrompt, "The Big Question")
iUR = Val(sUserResp)
If StrPtr(sUserResp) = 0 Then
Exit Sub
End If
Wend
2021-06-01 16:56:11
George O. Aljoe
this works if you have a set number of options to list, but what if you need to have a dynamic number of options? I am trying to make a macro to add building blocks more quickly, and I want to be able to 'set' which gallery (buildingblocktype) and 'category' my highlighted text will be added to when I create the building block. How can I make the options listed be the number of categories in a given buildingblock type?
2021-03-16 19:05:13
Randy O
To make the cancel button work, add this in before the "Select Case iUr" line:
If StrPtr(sUserResp) = 0 Then
Exit Sub
End If
2017-12-30 03:40:58
All the input boxes in the macros I create have a cancel button, however I cannot find anywhere the necessary macro code to activate this.
Would greatly appreciate a "tutorial" on this - for a beginner, please!
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 © 2023 Sharon Parq Associates, Inc.
Comments