Message Boxes

M

Margaret Upton

Please could someone help. I am trying to get a message box to give an A or
B option. In otherwords if you choose A it would save into Town or B into
Country. I only know how to get a Yes No situation. Could anyone help
please. I have put the example of what I am trying to get to work below.

Margaret

Dim intMsgBoxResult As Integer
intMsgBoxResult = MsgBox("Town or Country", vbYesNo + _
vbQuestion, "Current Status")
If intMsgBoxResult = vbYes Then

With Dialogs(wdDialogFileSaveAs)
.Name = "C:\My Documents\Town Details 2003\"
.Show
End With

Else

With Dialogs(wdDialogFileSaveAs)
.Name = "C:\My Documents\Country Details 2003\"
.Show

End With

End If
 
J

Jay Freedman

Hi, Margaret,

As you've already found out, the MsgBox feature allows only a few
varieties of buttons and doesn't let you change the text on the
buttons.

But you can make a userform (custom dialog) look like a message box,
and there you can create whatever buttons and labels you like.

For this, make a small userform (in the VBA editor, click Insert >
Userform, and drag the edges to message-box size). In the Properties
panel, rename the userform as frmTownCountry, and put Current Status
in the Caption box.

From the toolbox, add two command buttons and size them down a bit.
Give one of them the name btnTown, caption it Town, and give it the
accelerator t (that puts the underline under the T in the caption, and
lets the user use Alt+T as a shortcut). For the other button, name it
btnCountry, caption Country, accelerator c.

Press F7 (or View > Code) to go to the userform's code window, and
paste in this code:

Public Result As String

Private Sub btnCountry_Click()
Result = "Country"
Me.Hide
End Sub

Private Sub btnTown_Click()
Result = "Town"
Me.Hide
End Sub

Private Sub UserForm_Initialize()
Result = ""
End Sub

This says that when the user clicks the Town button, the variable
Result will get the string "Town" and the userform will disappear;
similarly for a click on the Country button. If the user clicks the X
in the title bar, the Result string will be empty.

Now edit your macro to this:

Dim myMsgBox As frmTownCountry
Set myMsgBox = New frmTownCountry
myMsgBox.Show

If myMsgBox.Result = "" Then ' canceled
Exit Sub
End If

If myMsgBox.Result = "Town" Then
With Dialogs(wdDialogFileSaveAs)
.Name = "C:\My Documents\Town Details 2003\"
.Show
End With
Else
With Dialogs(wdDialogFileSaveAs)
.Name = "C:\My Documents\Country Details 2003\"
.Show
End With
End If

Set myMsgBox = Nothing

This shows the userform instead of the original message box, and uses
the Result variable to decide what to show.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top