Re: Basic Newbie Question: What's the ":=" operator for?



termiflyer wrote:
> See if I interpret this correctly. These are the same:
>
> oDoc.Protect _
> Type:=wdAllowOnlyFormFields, _
> NoReset:=True
>
> and
>
> oDoc.Protect(wdAllowOnlyFormFields, True)
>
> Using named parameters only makes the code more readable, nothing
> else?

Not quite... there's one more wrinkle in there.

If you omit the parameter names, then you must have at least a comma for
every possible parameter up to the one just before the last value you
supply, and you have to put the values in the order specified by the syntax
diagram in the help. If you use the names, then you can include only the
ones you're actually giving values for, in any order.

As an example, take the MsgBox function. The full syntax is

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

Let's say I want to supply values for the prompt and title parameters, and
accept the defaults for buttons, helpfile, and context. If I supply
parameters by position (that is, without the names), then I have to write

MsgBox("Hello", , "My Title")

Notice the empty second comma, which serves as a placeholder for the buttons
parameter.

If I use the names, then I can put in just the prompt and title without
worrying about any of the default values, and I can even supply the values
out of order:

MsgBox(title:="My Title", prompt:="Hello")

One other thing: Use the parentheses around the parameters only if you're
going to use the return value of the function; if you're using it as a
statement, omit the parentheses. See
http://word.mvps.org/FAQs/MacrosVBA/BracketsWithArgmnts.htm for explanation.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org


.