Replace text in field code

P

Peter

I have some REF fields that point to bookmarks for document ranges that are
deleted in my VBA code. I want the REF field to point to a new bookmark. I
want to use something like the following

myRange.fields("REF OQTempRHMappingSection \r \h").code =
replace(myRange.fields("REF OQTempRHMappingSection \r \h").code, "TempRH",
"TempOnly")

Of course, this does not work or I wouldn't be writing this question.
Is there a way to accomplish what I'm trying to do? If I reveal codes in the
document, I can perform the edit manually. I think I should therefore be able
to do it in VBA as well, right?

Any help would be greatly appreciated.

Thanks,
Peter
 
J

Jay Freedman

You're close. The Code property of a field is a Range object. You need to
drill down one more level to ...Code.Text on both sides of the equal sign.

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

Peter Jamieson

You can always consider using find/replace, though in this case I think
you would have to ensure that all the relevant field codes were visible.

Otherwise, since you can't select the field on the basis of its code you
have to iterate through the appropriate collection to identify the field
you want to change - something along the lines of

dim f as field
for each f in activedocument.fields
if instr(ucase(f.code.text),"REF OQTEMP ETC.") > 0 then
' do what you need (see Jay's response)
end if
next

for the simple case where

Peter Jamieson

http://tips.pjmsn.me.uk
 

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