How to add MsgBox for recorded Macro in Word 2011

In the previous Macro for Word 2011 article, we saw the steps for editing a macro using VBA by changing the date and time alignment from Right to Center. In this post, we will see how to add a MsgBox to an existing Macro in Visual Basic Editor. Let us say you want to ask the user whether the alignment of date and time text needs to be changed.

Step 1: Click Tools menu, select Macro followed Macros submenu.

Macros option in Word 2011 for Mac

Step 2: In the Macros window, select the Macro where you want to add the MsgBox then click Edit button.

Macros Editor in Word 2011 for Mac

Step 3: Navigate to the line before the statement that sets the alignment for the paragraph to Center.

Visual Basic Editor Window in Word 2011 for Mac

Step 4: Add the following piece of MsgBox code.

MsgBox(“Do you want the date and time to be center aligned?”,vbYesNo,”Date and Time alignment”)

Where Prompt = Do you want the date and time to be center aligned?, this is the message that needs to be displayed to the user.

vbMsgBoxStyle = vbYesNo, which style of button to be displayed. We are going with Yes and No.

Title = “Date and Time alignment”, title for the message box.

Step 5: Next we need to find out the selection made by the user. If Yes is selected then the alignment will be changed and in case of No the default alignment will be maintained. This is achieved in VBA using the If Then End If statement as show below.

MsgBox in VBA

Step 6: Now save and run the modified Macro. This time you will be prompted with the following message box And the alignment will happen only when you click the Yes button.

Message Box displayed in Word 2011 for Mac

In Category: Apple, Mac, MS Office

Ravi Shankar

A Software developer and blogger who is always looking to provide technical help to the wider community.

Show 0 Comments
No comments yet. Be the first.

Leave a Comment

Get your free copies of the following tech guides by joining the Digital Answers mailing list.