Modifying field contents using vba

T

toth.usenet

hi,

i've got a word document which has been generated automatically and
the references/links/sources for table of contents and index have been
generated using field information. so a header would look like this

1.1. Header\Title\Chapter { XE "Header\Title\Chapter" }

i have a vba script which will modify the header text just as desired
(e.g. to Chapter). however, i would also need to modify the content
of the field to also be only { XE "Chapter" } for it to show in the
table of contents and in the index. how do i go about changing the
text inside the fields?

one possibility is to modify the string inside the field, the other is
to copy the header text and paste it into the field. unfortunately i
don't have any experience with fields at all so i couldn't tell which
one is easier, and being a newbee to vba in general i wouldn't know
how to implement either or them.

any help is appreciated.

cheers,
thomas
 
D

David Sisson

hi,

i have a vba script which will modify the header text just as desired
(e.g.  to Chapter). however, i would also need to modify the content

Show us the relevant code.
 
T

toth.usenet

hi david,

sorry for the late reply, i can't always access the newsgroups.

the code i have is the following:

Dim oPrg As Paragraph
Dim rTmp As Range
For Each oPrg In ActiveDocument.Paragraphs
Set rTmp = oPrg.Range
With rTmp.Find
.Text = "\"
.Forward = False '!
While .Execute
rTmp.Start = oPrg.Range.Start
rTmp.Select ' for testing
rTmp.Text = ""
Wend
End With
Next

i hoped that i could re-use the same code but using
ActiveDcoument.Field instead of the paragraphs. however, when i tried
it it failed which i'm not surprised about as i have the feeling a
slightly different approach would need to be taken. any ideas?

thanks for your help,
thomas
 
D

David Sisson

Try this

Sub ReplaceXETextInFields()

Dim aDoc As Document
Dim MyField As Field

Set aDoc = ActiveDocument
For Each MyField In aDoc.Fields
If MyField.Type = wdFieldIndexEntry Then
MyField.Select
With Selection.Find
.Text = "Header\Title\Chapter"
.Replacement.Text = "Chapter"
.Forward = True
.Wrap = wdFindStop
.Format = False
.Execute Replace:=wdReplaceAll
End With
End If
Next
End Sub
 

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