Find and replace text within index codes

D

Dale

Hi, I have a huge document with index codes. I want to do some global find
and replaces on the text in some of the index codes. The problem is that
some of this same text occurs outside the index codes in the body of the
document and I don’t want to change that. Is there a way to do a find and
replace and specify that only text within the index codes gets replaced?
I’ve experimented with some things on my own using ^d and ^19, as well as
specifying a search for hidden text, but so far nothing has worked. Is there
a way to do this, if not using a regular built in Word feature then maybe
with a macro? Thanks in advance for any help.
 
S

Suzanne S. Barnhill

Unless the text appears as Hidden outside the XE field codes, you should be
able to search for the text in question, formatted as Hidden. You'll have to
display Hidden text for Find to find it, though.
 
G

Graham Mayor

The following macro will do that: http://www.gmayor.com/installing_macro.htm
Note the search and replacement are case sensitive.

Sub ReplaceIndex()
Dim iFld As Long
Dim fText, rText As String
fText = InputBox("Enter text to be found", "Find Text")
rText = InputBox("Enter text to replace found text", "Replace Text")
Selection.HomeKey
ActiveDocument.ActiveWindow.View.ShowFieldCodes = True
For iFld = 1 To ActiveDocument.Fields.Count
With ActiveDocument.Fields(iFld)
If .Type = wdFieldIndexEntry Then
If InStr(1, .Code, fText) <> 0 Then
.Code.Text = Replace(.Code.Text, fText, rText)
End If
.Update
End If
End With
Next iFld
ActiveDocument.ActiveWindow.View.ShowFieldCodes = False
End Sub

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Dale

Hi, Suzanne, thanks for your reply. As it turns out, I had my show/hide on
but I didn't specifically have "hidden text" checked in Word Options,
Display. I checked the box and it now works. :) Thank you so much!
 
D

Dale

Hi, Suzanne, I just tried the find and replace again and either I had some
other option checked when I first did it or else maybe it didn't work as I
thought it did at first because now I can't get it to work again, even with
the hidden text displayed.

I can use Graham's macro to do the find and replace, but I'm just wondering
what other option I might have had checked or unchecked that seemed to allow
it to work the first time but not again. Or maybe I was mistaken and it just
replaced the visible matches and I thought it worked on the hidden text. I
guess I'm trying to do too many things at once. :} Sorry for the confusion,
but thanks in advance for any information. :)
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
S

Suzanne S. Barnhill

If you have Hidden text displayed (which you would have to have to see the
XE field codes) and have specified Hidden formatting for the search, then it
should work.
 
D

Dale

Hi, Suzanne, thanks for your response. I've been experimenting and I can't
get it to work on the text in the index codes. I tried formatting some text
outside the index codes as hidden text and it works on that. It just doesn't
seem to recognize the hidden text within the index codes. Right now I have
the following options checked in Word Options, Display: Hidden text, Show
all formatting marks. Is there another option I have to have checked
somewhere? I'm using Word 2007. I'm not sure if that would make a
difference. I'm still not used to 2007. I feel like I'm probably missing
something really obvious.

Thanks for your help! :)
 
S

Suzanne S. Barnhill

I suspect that Word is just not treating XE fields as "simple" Hidden text.
You can search for fields, as you know, with ^d or ^19, but that type of
search requires that you be performing some action on the entire field. And
you can search for the text within fields by typing the field code (in this
case XE) along with the text to be replaced, which doesn't require that you
specify Hidden text. This does demonstrably work (I just tried it). You do
have to have Hidden text displayed so Word can find the XE fields, of
course.

My replace operation was as follows:

Find what: XE "an index entry"
Replace with : XE "another index entry"

Obviously, this isn't much help if you're trying to replace just one part of
many field, replacing "Smith" with "Jones," for example, in fields such as
{ XE "Mary Smith" }, { XE "John Smith" }, and so on (though you might be
able to accomplish this with wildcards). OTOH, if if you have { XE
"Smith:Mary" } and { XE "Smith:John"), you could search for just XE "Smith
and replace with XE "Jones.
 
D

Dale

Hi, Suzanne, thanks for the information. I tried your suggestion using XE
and a word I was searching for and that did work. This is the first time
I've used index codes extensively so I'm learning a lot. Thank you very much
for all your help. :)
 

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