How to create input and message boxes in a Word document with VBA?


Last update: October 2019; 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.

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

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:

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:

1 comments
ahmad says September 26, 2019

Hi,can the default value in inputbox to be a pasted value from clipboard without using of ctrl v?

Reply
Add Your Reply