altering field codes

F

fred kruger

can anyone tell me if it is possible to alter field codes
with vba.
i hav a document which has a picture inserted in the
first page and the header of everysubsequent page and the
field code for it is as follows.

{INCLUDEPICTURE "C:\\Program Files\\Microsoft
Office\\Clipart\\WYP\\SIMPLAND.TIF" \* MERGEFORMAT \d }

depending on a user selection this picture may change
from simpland.tiff to kmc.tiff. is there a way of just
changing the field without having to completely reinstall
the picture each time or is it best jsut to reinstall the
picture.

I can code for the reinsert so thats not a problem just
wanted to know if the field code could be altered.

cheers fred
 
D

Doug Robbins - Word MVP

Hi Fred,

The following is a bit more complex than what you will need, but shows you
how to go about changing a field code:

' Macro created 26/10/01 by Doug Robbins to update links in a document
'
Dim alink As Field, linktype As Range, linkfile As Range
Dim linklocation As Range, i As Integer, j As Integer, linkcode As Range
Dim Message, Title, Default, Newfile
Dim counter As Integer


counter = 0
For Each alink In ActiveDocument.Fields
If alink.Type = wdFieldLink Then

Set linkcode = alink.Code
i = InStr(linkcode, Chr(34))
Set linktype = alink.Code
linktype.End = linktype.Start + i
j = InStr(Mid(linkcode, i + 1), Chr(34))
Set linklocation = alink.Code
linklocation.Start = linklocation.Start + i + j - 1
If counter = 0 Then
Set linkfile = alink.Code
linkfile.End = linkfile.Start + i + j - 1
linkfile.Start = linkfile.Start + i
Message = "Enter the modified path and filename following this
Format " & linkfile
Title = "Update Link"
Default = linkfile
Newfile = InputBox(Message, Title, Default)
End If
linkcode.Text = linktype & Newfile & linklocation
counter = counter + 1
End If
Next alink

Basically, you set a .Range equal to the field code, then modify the .Text
property of the .Range.

Please respond to the newsgroups for the benefit of others who may be
interested.

Hope this helps
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