VB Code formats

S

sestyd

Can someone tell me what the deal is with this format
(just pretend no wraps):

..Paragraphs(2).Range.InlineShapes.AddPicture
FileName:=strImageName, LinkToFile:=False,
SaveWithDocument:=True
(which I have never before encountered - is it really a
method call? does it actually return a value?)

as opposed to this format:

Dim thisShape As InlineShape
Set thisShape = .Paragraphs
(2).Range.InlineShapes.AddPicture(strImageName, False,
True)

Vera
 
J

Jezebel

The two formats do the same thing. There are two differences.

1) The first uses named arguments, the second doesn't. This is a matter of
programming preference; but using named arguments is generally considered
good practice. It's more readable, and usually you need include only the
arguments relevant to the task in hand. If you don't include the argument
names you have to get the positions right, which can be tricky if there are
many arguments. There are some (old) VB functions that do not support named
arguments.

2) The second version retrieves the reference to the added picture. This is
a programming choice. The function itself adds the picture to the document
and returns a reference to it. If you just want to add the picture then the
first version is fine; if you want to add the picture and then, in code, do
something with it, use the second.
 
P

Perry

Not using the parameter names, you'll have to stick to
the sequence in which the parameters are passed to
a function/routine.

for starters:
AddPicture expects 3 parameters in a particular sequence.

If you want to set the 'SaveWithDocument' parameter to True

following code will work
<> .InlineShapes.AddPicture(strImageName, , True)

Following line will not set the 'SaveWithDocument' param to True
Instead, it will set LinkToFile to True:
Y're passing 2 parameters and without the reference/name, the value is
passed to
second parameter in sequence, namely: LinkToFile (and not to:
SaveWithDocument)
<> .InlineShapes.AddPicture(strImageName, True)

Following line does set the 'SaveWithDocument' param to True
despite of the fact that y're passing only 2 parameters
<> .InlineShapes.AddPicture(FileName:=strImageName, SaveWithDocument:=True)

Note: you can only skip parameters from being passed, if they're optional !!

Krgrds,
Perry
 
V

vera

Jezebel,

Thanks! That was helpful. Probably using the named
arguments will optimize the code somewhat. I guess I'm
very used to getting the positions right, I've never used
the named arguments format ever, and I've been programming
a long time!

Vera
 

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