Msg Box Prompts

G

Greg Maxey

I have been fumbling around with macros for about a year now and every time
I try to put together a msgbox I pull my hair out with the structure i.e.
trying to get the " and the $ and the , and the & in the right place as in
the example below

MsgBox "This document contains " & .Sentences.Count & " sentences", , _
"Sentence Count"

I have searched in help to no avail for a tutorial on building these
constructions to no avail. Looking for a shove or gentle push in the right
direction.

Thanks
 
J

Jay Freedman

I have been fumbling around with macros for about a year now and every time
I try to put together a msgbox I pull my hair out with the structure i.e.
trying to get the " and the $ and the , and the & in the right place as in
the example below

MsgBox "This document contains " & .Sentences.Count & " sentences", , _
"Sentence Count"

I have searched in help to no avail for a tutorial on building these
constructions to no avail. Looking for a shove or gentle push in the right
direction.

Thanks

Hi Greg,

With pleasure...

Start with the syntax listed in the help for MsgBox (ignoring the
helpfile and context parameters, which are rarely used):

MsgBox(Prompt[, Buttons] [, Title])

(As always, the square brackets mean the parameter is optional.) The
parentheses are needed only if you're using MsgBox as a function,
using its return value.

The Prompt parameter is a string, the text that appears in the body of
the messagebox. It can be a single literal string (a sequence of
characters surrounded by quotes), a single variable of String type, or
a mixture of both stuck together with & (the concatenation operator):

MsgBox "This is some text"
MsgBox myString
MsgBox "This is some text " & myString

The example you gave confuses things a little, since .Sentences.Count
is an integer. VBA will implicitly do the conversion to a string in
order to build the parameter; you could do it explicitly by writing
CStr(.Sentences.Count). In any case, the Prompt parameter in your
example is this part:

"This document contains " & .Sentences.Count & " sentences"

which consists of two literals surrounding the number value.

If you include either of the optional parameters, you also need the
commas that separate them. In your example, you're omitting the
Buttons parameter but including the Title parameter (another string),
so you need two commas to tell VBA that. This is called passing
parameters by position. For human understanding it's better to use
named parameters -- but you need to remember to use the special
parameter assignment symbol := instead of the regular assignment =
like this:

MsgBox Prompt:="This is text", Title:="Sample"

When you use named parameters, they can be in any order, and you don't
need extra commas when you omit an optional parameter.

If you want to use the Buttons parameter, it's a numeric expression
built up by adding together values. Some actually do specify which
buttons will appear, others control which icon appears to the left of
the message, and some can control which button is the default and
whether the box is modal (i.e., whether it can lose focus). These
numbers all have names as constants, which are listed in the help
topic.

When you specify buttons other than OK, you can test the return value
of the function to find out which button was pressed. The return
values also have named constants, so you can do something like this:

Dim answer As Integer
answer = MsgBox(Prompt:="Really quit?", _
Buttons:=vbYesNo + vbQuestion)
If answer = vbYes Then Exit Sub
 
G

Greg Maxey

Jay,

Thanks for the explanation.
<(the concatenation operator)
This is the key to the kingdom that I was looking for. I searched for
operator in help and & operator popped up. I'm off to the races.


--
Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Jay said:
I have been fumbling around with macros for about a year now and
every time I try to put together a msgbox I pull my hair out with
the structure i.e. trying to get the " and the $ and the , and the &
in the right place as in the example below

MsgBox "This document contains " & .Sentences.Count & " sentences",
, _ "Sentence Count"

I have searched in help to no avail for a tutorial on building these
constructions to no avail. Looking for a shove or gentle push in
the right direction.

Thanks

Hi Greg,

With pleasure...

Start with the syntax listed in the help for MsgBox (ignoring the
helpfile and context parameters, which are rarely used):

MsgBox(Prompt[, Buttons] [, Title])

(As always, the square brackets mean the parameter is optional.) The
parentheses are needed only if you're using MsgBox as a function,
using its return value.

The Prompt parameter is a string, the text that appears in the body of
the messagebox. It can be a single literal string (a sequence of
characters surrounded by quotes), a single variable of String type, or
a mixture of both stuck together with & (the concatenation operator):

MsgBox "This is some text"
MsgBox myString
MsgBox "This is some text " & myString

The example you gave confuses things a little, since .Sentences.Count
is an integer. VBA will implicitly do the conversion to a string in
order to build the parameter; you could do it explicitly by writing
CStr(.Sentences.Count). In any case, the Prompt parameter in your
example is this part:

"This document contains " & .Sentences.Count & " sentences"

which consists of two literals surrounding the number value.

If you include either of the optional parameters, you also need the
commas that separate them. In your example, you're omitting the
Buttons parameter but including the Title parameter (another string),
so you need two commas to tell VBA that. This is called passing
parameters by position. For human understanding it's better to use
named parameters -- but you need to remember to use the special
parameter assignment symbol := instead of the regular assignment =
like this:

MsgBox Prompt:="This is text", Title:="Sample"

When you use named parameters, they can be in any order, and you don't
need extra commas when you omit an optional parameter.

If you want to use the Buttons parameter, it's a numeric expression
built up by adding together values. Some actually do specify which
buttons will appear, others control which icon appears to the left of
the message, and some can control which button is the default and
whether the box is modal (i.e., whether it can lose focus). These
numbers all have names as constants, which are listed in the help
topic.

When you specify buttons other than OK, you can test the return value
of the function to find out which button was pressed. The return
values also have named constants, so you can do something like this:

Dim answer As Integer
answer = MsgBox(Prompt:="Really quit?", _
Buttons:=vbYesNo + vbQuestion)
If answer = vbYes Then Exit Sub
 
J

Jay Freedman

Jay,

Thanks for the explanation.
<(the concatenation operator)
This is the key to the kingdom that I was looking for. I searched for
operator in help and & operator popped up. I'm off to the races.

Sounds good (but don't bet your shirt just yet :).
 

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