How to easily insert Word input and message boxes?


Last update:September 2018; applicability: Office 2019, 2016, 2013.

Sometimes, we need to make our Word documents interactive and capture some user input. For that we typically use both message and input boxes .

As the name suggests a message box is a piece of information that is given to the users. Also input box is something where user’s given input is stored in a variable and used to give an output. This post has all the information and code you’ll need to create fully functional documents with VBA message and input boxes for 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 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.

2018-01-17 16_02_28-



  • Highlight your Document.
  • Click Insert and then select Module.

2018-01-17 16_03_01-Microsoft Visual Basic for Applications - Document1

  • 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.

2018-01-16 14_24_13-

2018-01-16 14_24_44- 2018-01-16 14_25_13-

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

Sub messagebox()
'Variables assigned
Dim buttonclick As VbMsgBoxResult
'Message box to display information to user
MsgBox "Hi, Visual basic is easy to work with", vbExclamation, "VBA"
'Message box to ask question to user with yes or no question
buttonclick = MsgBox("Do you like VBA?", vbQuestion + vbYesNo, "VBA")
If buttonclick = vbYes Then
MsgBox "Yes, they are great!", vbExclamation, "VBA"
Else
MsgBox "Why not? They are great!", vbCritical, "VBA"
End If
End Sub

Output:

2018-01-16 16_28_08-VBA 2018-01-16 16_28_26-VBA

2018-01-16 16_28_43-VBA 2018-01-16 16_29_05-VBA

Input Boxes VBA code

Input box is used to get information from the user. Let us discuss about it with 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:

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:

2018-01-16 16_30_13-Question 1 2018-01-16 16_30_30-Question 2

2018-01-16 16_30_59-Microsoft Word

Leave a Comment: