IncludePicture Field use with VBA

S

Sean

Is it possible to set an IncludePicture field in a
document and the set the path as a variable that could be
changed by using code? If so what would the code look
like to add the field with a variable as the Filename?

Thanks.
 
J

Jay Freedman

Sean said:
Is it possible to set an IncludePicture field in a
document and the set the path as a variable that could be
changed by using code? If so what would the code look
like to add the field with a variable as the Filename?

Thanks.

Hi Sean,

This is just a little demo. Notice that if the filename in the field
code contains any backslashes, they have to be doubled.

Sub foo()
Dim strFN As String
Const wdQuote = """"

' There are many ways to get the
' filename of the picture. This is
' the worst for the user, but short code.
strFN = InputBox$("Enter picture's filename")
strFN = Replace(strFN, "\", "\\")

If Len(strFN) > 0 Then
ActiveDocument.Fields.Add _
Range:=Selection.Range, _
Type:=wdFieldIncludePicture, _
Text:=wdQuote & strFN & wdQuote, _
preserveformatting:=False
End If
End Sub

If you mean that you want to have the field include one picture now
and change that (in the same document) to a different picture later,
that's another story... then you want to display field codes and do a
find/replace of the old filename with the new one.
 
M

macropod

Hi Sean,

This can also be done by inserting a FILLIN field into the INCLUDEPICTURE
field. No vba required - unless you want to use
ActiveDocument.Fields.Update, but you could get the same result by printing
the document with 'update fields' checked, under Tools|Options|Print.

Cheers
 
D

dynamictiger

I am intending adding photos to word from MS access code. Your code

Code
-------------------
ActiveDocument.Fields.Add _
Range:=Selection.Range, _
Type:=wdFieldIncludePicture, _
Text:=wdQuote & strFN & wdQuote, _
preserveformatting:=False

-------------------


works, however it is adding a field to my template which is not what
intended to do.
If you mean that you want to have the field include one picture now
and change that (in the same document) to a different picture later,
that's another story... then you want to display field codes and do a
find/replace of the old filename with the new one.

Defines exactly what I want to do.

I have an existing picture acting as marker in my document, it i
inserted as an includepicture field. This is the field that I want t
update with a picture from code. I have attempted searching for hel
on this and have failed to find relevant references. The closest i
your post. Can you help please
 
D

Doug Robbins - Word MVP

Try adding a line

Selection.Range.Fields(1).Unlink

Or to add a picture to a document, you can use:

..Range.InlineShapes.AddPicture Filename:=strFN

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

dynamictiger

Thanks for your advice.

Selection.Range.Fields(1).Unlink - Is giving me an error on the wor
merge field doc and the the other doc I have set up without a field.
The error is "Not part of the collection".

..Range.InlineShapes.AddPicture Filename:=strFN - Works fine but i
still placing the picture at the top of the document not in th
predetermined picture box.

Is there something I am missing on the word side
 
D

Doug Robbins - Word MVP

You need to make a reference to the .Range into which you want the picture
to be inserted.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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