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.
Step 2: In the Macros window, select the Macro where you want to add the MsgBox then click Edit button.
Step 3: Navigate to the line before the statement that sets the alignment for the paragraph to Center.
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.
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.