Find/Replace Field Code

M

MattW

I have documents with field codes and I need to find/select the field codes
based on thier name and then replace with plain text.

I can't seem to find any documentation on how to select a field code... I
won't be able to specify the exact name for the field code. The documents I
am working with may have the same field code as { OrdClosingDate_1_4_0 } or
{ OrdClosingDate_2_4_0 } or { OrdClosingDate_3_4_0 } etc, and I will need to
find them by specifying "OrdClosingDate" and replace (all matching
occurances with a text string from a variable that I retrieved from an SQL
server... The field code should be deleted in the process.

Any insight on this would be greatly appreciated. I am currently using the
code below to find/replace, so maybe i just need to know what to specify for
'TagToReplace'

Thanks, Matt
___________________________________________________

Function InsertData(TagToReplace, DataToInsert)

If DataToInsert = "" Then
Exit Function
End If

Selection.HomeKey wdStory
Selection.Find.ClearFormatting

With Selection.Find
.Text = TagToReplace
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With

Do Until Selection.Find.Execute = False ' Start Loop

Selection.Find.ClearFormatting

With Selection.Find
.Text = TagToReplace
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
End With

Selection.TypeText Text:=DataToInsert ' ParagraphNumber & "."

If DataToInsert = " " Then Selection.TypeBackspace

Loop

End Function
 
D

Doug Robbins - Word MVP

I am assuming that these are not "field codes" as that term is used in Word.
That is, the { OrdClosingDate_1_4_0 } is not displayed by toggling on the
display of field codes. If that assumption is correct, the following will
replace each instance of a construct such as { OrdClosingDate_1_4_0 } with
"your new data"

Dim ftext As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="OrdClosingDate", _
MatchWildcards:=False, Forward:=True, Wrap:=wdFindStop) = True
Set ftext = Selection.Range
ftext.Start = ftext.Start - 2
ftext.End = ftext.End + 8
ftext.Text = "your new data"
Loop
End With


--
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
 
M

MattW

Well that's alot more than i could get it to do...

This Line: ftext.Text = "NewData" Caused this error: Run-time error '6028':
The range cannot be deleted.

Also will that code work if the length changes?, ex: instead of {
OrdClosingDate_1_4_0 } what if it was { OrdClosingDate_11_43_10 }

-Matt
 
D

Doug Robbins - Word MVP

It worked fine for me when I copied this part:

I can't seem to find any documentation on how to select a field code... I
won't be able to specify the exact name for the field code. The documents I
am working with may have the same field code as { OrdClosingDate_1_4_0 } or
{ OrdClosingDate_2_4_0 } or { OrdClosingDate_3_4_0 } etc, and I will need
to
find them by specifying "OrdClosingDate" and replace (all matching
occurances with a text string from a variable that I retrieved from an SQL
server... The field code should be deleted in the process.

of your original post into a new document and ran it on that document.

In its present form it will not handle { OrdClosingDate_11_43_10 }
But, with the following modified lines of code, it should handle both

Do While
..Execute(findText:="OrdClosingDate_[0-9]{1,}_[0-9]{1,}_[0-9]{1,}", _
MatchWildcards:=True, Forward:=True, Wrap:=wdFindStop) = True
Set ftext = Selection.Range
ftext.Start = ftext.Start - 2
ftext.End = ftext.End + 2


--
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
 
M

MattW

I'm getting the same error... I checked again and I think this is a Field
Code I'm replacing, I'm not very familiar with them but if I go to
Tools>Options>Show and Check the field codes box then it shows up as "{
OrdClosingDate_1_4_0 }" If I uncheck field codes then it shows up as "Error!
No bookmark name given"

Here is the code I used:

Sub test123()

Anything = ReplaceElement("OrdClosingDate", "You Have Been Replaced")

End Sub

Function ReplaceElement(ElementName, DataToInsert)

Dim ftext As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting

With Selection.Find
Do While .Execute(findText:=ElementName &
"_[0-9]{1,}_[0-9]{1,}_[0-9]{1,}", _
MatchWildcards:=True, Forward:=True, Wrap:=wdFindStop) = True
Set ftext = Selection.Range
ftext.Start = ftext.Start - 2
ftext.End = ftext.End + 2
ftext.Text = DataToInsert
Loop
End With

End Function
 
M

MattW

I was able to get it working, I noticed that after the script crashed before
I could move the cursor right 3 times and backspace twice to delete the field
code and then type whatever... so i changed the code to do that... seems to
work fine... Thank you for your help

With Selection.Find
Do While .Execute(findText:=FieldCodeName &
"_[0-9]{1,}_[0-9]{1,}_[0-9]{1,}", MatchWildcards:=True, Forward:=True,
Wrap:=wdFindStop) =True
Set ftext = Selection.Range
Selection.MoveRight Unit:=wdCharacter, Count:=3
Selection.TypeBackspace
Selection.TypeBackspace
Selection.TypeText (DataToInsert)
Loop
End With
 
D

Doug Robbins - Word MVP

OK, while it is not a valid field code, it looks like the text is inserted
inside field delimiters. In that case, try this:

Dim afield As Field
With ActiveDocument
For Each afield In .Fields
If Left(LTrim(afield.Code), 14) = "OrdClosingDate" Then
afield.Select
Selection.Range.Text = "your new data"
End If
Next afield
End With

--
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
 
M

MattW

That works perfect, thanks again

Doug Robbins - Word MVP said:
OK, while it is not a valid field code, it looks like the text is inserted
inside field delimiters. In that case, try this:

Dim afield As Field
With ActiveDocument
For Each afield In .Fields
If Left(LTrim(afield.Code), 14) = "OrdClosingDate" Then
afield.Select
Selection.Range.Text = "your new data"
End If
Next afield
End With

--
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

MattW said:
I'm getting the same error... I checked again and I think this is a Field
Code I'm replacing, I'm not very familiar with them but if I go to
Tools>Options>Show and Check the field codes box then it shows up as "{
OrdClosingDate_1_4_0 }" If I uncheck field codes then it shows up as
"Error!
No bookmark name given"

Here is the code I used:

Sub test123()

Anything = ReplaceElement("OrdClosingDate", "You Have Been Replaced")

End Sub

Function ReplaceElement(ElementName, DataToInsert)

Dim ftext As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting

With Selection.Find
Do While .Execute(findText:=ElementName &
"_[0-9]{1,}_[0-9]{1,}_[0-9]{1,}", _
MatchWildcards:=True, Forward:=True, Wrap:=wdFindStop) =
True
Set ftext = Selection.Range
ftext.Start = ftext.Start - 2
ftext.End = ftext.End + 2
ftext.Text = DataToInsert
Loop
End With

End Function
 

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