How to easily insert message and input boxes into a Microsoft Word 2016 doc?


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

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. In this post we will be discussing about how to create both with some easy to understand VBA code.

Here’s how to add the VBA code provided in this post into Microsoft Word:

  • First off, enable the developer tab.
  • Click 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 provided below into the Module you just created into your module.
  • Click on Save. In case it’s a new file’ you’ll need to provide a name.
  • Close the Visual Basic Editor.

Now, let’s learn about the Message box:

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.

 

Here’s some 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

Now let’s learn about Input boxes:

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: