Word 2003: put a field in an IncludePicture field

J

Jose Valdes

In Word 2003, I'd like my VBA code to create an IncludePicture field that
contains a field called "DOCPROPERTY home_directory" as part of the path to
the external graphic file.
Current line of code: Selection.InlineShapes.AddPicture fileName:=
"c:\Directory\file_name.jpg", LinkToFile:=True, SaveWithDocument:=False
Current result: { INCLUDEPICTURE "c:\\Directory\file_name.jpg" \*
MERGEFORMAT \d }
Desired result: { INCLUDEPICTURE "{ DOCPROPERTY
home_directory }\file_name.jpg" \* MERGEFORMAT \d }
Plan A that I am considering: allow the VBA code to create the link and do a
find/replace to change "c:\\Directory\file_name.jpg" to { DOCPROPERTY
home_directory }.
Plan B: find a DocProperty collection, assign home_directory to a variable,
and use variable in the AddPicture command.
Should I use Plan A or B? Or another plan? I know how to do A, but it seems
like the long way of solving the problem. I don't know whether plan B is
possible, but (if possible) it would require fewer lines of code. Any
thoughts?
 
J

Jose Valdes

Jay, thanks for the reply. Forgive me for pressing my question further and
showing my ignorance of Word VBA.

I was under the impression that both Plan A and B would result in an
IncludePicture field that contains the DocProperty field called
"home_directory". The result of either plan would require the same amount of
support work that you described below:
With Plan A, you just go to File > Properties > Custom and change the
home_directory value there, then do a field update.

However, you explained that Plan B would result in a hard-coded path for
each instance of the IncludePicture field. Does this mean that the
AddPicture command permits only a hard-coded directory path?
 
J

Jay Freedman

I think the misunderstanding crept in when I read "variable" in your
description of plan B. There is no such thing as a variable in the body of a
document -- that's the purpose served by fields. I assumed you meant to
extract the value of the custom document property into a variable in the
macro, and then write that value into the IncludePicture field codes. As I
now understand your proposal, plan B is not possible (or, in another sense,
plan A and plan B are exactly the same because the "variable" is the
DocProperty field).

Plan A will work, provided that the value in the custom document property
contains doubled backslashes in the path.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Jose Valdes

Thanks Jay!

Jay Freedman said:
I think the misunderstanding crept in when I read "variable" in your
description of plan B. There is no such thing as a variable in the body of
a document -- that's the purpose served by fields. I assumed you meant to
extract the value of the custom document property into a variable in the
macro, and then write that value into the IncludePicture field codes. As I
now understand your proposal, plan B is not possible (or, in another sense,
plan A and plan B are exactly the same because the "variable" is the
DocProperty field).

Plan A will work, provided that the value in the custom document property
contains doubled backslashes in the path.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup
so all may benefit.
 

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