Applicability: Office 365,2019, 2016, 2013.
Sometimes, we would like to make our Word documents more interactive and even capture some user input. Enter message and input boxes .
As the name suggests a message box is a piece of information that is displayed to the document users. Input boxes are instrumental to capture information from the user.
This post has all the information and code samples you’ll need to create fully functional documents with Visual Basic for Applications (VBA) message and input boxes for Microsoft Word.
How to add VBA code into Word?
Before we go ahead and explain how to add message and input boxes, here’s a quick refresher on how to add your custom VBA code to a Word document.
- First off, go ahead and enable the Developer tab.
- Now, hit the Visual Basic button from the Developer tab. This will open the Visual Basic Editor.
- Highlight your Document.
- Click Insert and then select Module.
- Copy the code we provide below into the Module you just created into your module.
- Click on Save. Provide a name to your document if prompted.
- Close the Visual Basic Editor.
Message Boxes VBA code
Syntax:
MsgBox(prompt,buttons,title,helpfile,context)
Syntax explanation:
Prompt:
Required. It is the information that can be given to the user. It should be inserted inside the double quotation mark.
Buttons:
Optional. Buttons are those kind of buttons that are given to the users information. There are various buttons available for MsgBox() and they can be found on the pictures below.
Title:
Optional. It is the title for the information given to the user. If not given, Microsoft word will be the default title.
Helpfile and context:
- Optional. Helpfile will identify the Help file available in local disk which can be provided as a help in a dialog box
- Optional. Context is numeric expression which is used to identify the help topic by the unique number.
Sample Message box code
'VBA
Sub messagebox()
'Variables assigned
Dim buttonclick As VbMsgBoxResult
'Message box to display information to user
<strong>MsgBox "Hi, Visual Basic is easy to work with", vbExclamation, "VBA"</strong>
'Message box to ask question to user with yes or no question
<strong>buttonclick = MsgBox("Do you like VBA?", vbQuestion + vbYesNo, "VBA")</strong>
If buttonclick = vbYes Then
<strong>MsgBox "Yes, they are great!", vbExclamation, "VBA"</strong>
Else
<strong>MsgBox "Why not? They are great!", vbCritical, "VBA"</strong>
End If
End Sub
Output:
Dialog and Input Boxes VBA code
Input box is a simple dialog that allows us to prompt the user for some information. Let us get acquainted with the VBA code using an example.
Syntax:
InputBox(Prompt, Title, Default, XPos, YPos, HelpFile, HelpContextId, Type)
Syntax explanation:
Prompt:
Required. It is the information that can be given to the user. It should be inserted inside the double quotation mark.
Title:
Optional. It is the title for the information given to the user. If not given, Microsoft word will be the default title.
Default:
Optional. It displays the default value inside the text box, before user types for a value. If not specified, the text box will be empty.
XPos:
Optional. It is used as the position on the X axis. From left to right
YPos:
Optional. It is used as the position on the Y axis. From top to bottom
Helpfile and HelpContext ID:
- Optional. Helpfile will identify the Help file available in local disk which can be provided as a help in a dialog box
- Optional. HelpContext ID is the id number for the help file.
Input box code:
'VBA
Sub macro()
'Variables assigned
Dim a As Long
Dim b As Long
'Input box to get value from user
a = InputBox("Enter a value for a", "Question 1")
b = InputBox("Enter a value for b", "Question 2")
'Answer displayed in a message box
MsgBox ("Answer is " & Val(a) + Val(b))
End Sub
Output:
Hi,can the default value in inputbox to be a pasted value from clipboard without using of ctrl v?