From: LaRana6261 on
Hello,
I need a macro for a .doc that will find certain text and replace it with
its corresponding value from a table of values.
Examples:
Text = s[32] cross reference "Record Name"
Text =s[33] cross reference "Record Address"

My table has about 190 record names which I need to replace everytime the
field "Code" is found. In the example above, s[32] will be replace by "record
name". can someone assist?

Thanks,
--
EdV
From: Graham Mayor on
The following should work. It uses a two column table, without blank rows,
here saved as a document - D:\My Documents\Test\Changes.doc The document
name is not relevant, as long as you define it in the macro. Put the text to
be found in the first column and the replacement in the second column and
run the macro with the document to be edited active.

Sub ReplaceFromTableList()
Dim oChanges As Document, oDoc As Document
Dim oTable As Table
Dim oRng As Range
Dim rFindText As Range, rReplacement As Range
Dim i As Long
Dim sFname As String
sFname = "D:\My Documents\Test\Changes.doc"
Set oDoc = ActiveDocument
Set oRng = oDoc.Range
Set oChanges = Documents.Open(sFname)
Set oTable = oChanges.Tables(1)
oDoc.Activate
For i = 1 To oTable.Rows.Count
Set rFindText = oTable.Cell(i, 1).Range
rFindText.End = rFindText.End - 1
Set rReplacement = oTable.Cell(i, 2).Range
rReplacement.End = rReplacement.End - 1
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findText:=rFindText, _
MatchWholeWord:=True, _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindContinue) = True
oRng.Text = rReplacement
Loop
End With
Next i
oChanges.Close wdDoNotSaveChanges
End Sub


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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


"LaRana6261" <LaRana6261(a)discussions.microsoft.com> wrote in message
news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8(a)microsoft.com...
> Hello,
> I need a macro for a .doc that will find certain text and replace it with
> its corresponding value from a table of values.
> Examples:
> Text = s[32] cross reference "Record Name"
> Text =s[33] cross reference "Record Address"
>
> My table has about 190 record names which I need to replace everytime the
> field "Code" is found. In the example above, s[32] will be replace by
> "record
> name". can someone assist?
>
> Thanks,
> --
> EdV


From: LaRana6261 on
This is great! I can work with this!

Thank you,
--
EdV


"Graham Mayor" wrote:

> The following should work. It uses a two column table, without blank rows,
> here saved as a document - D:\My Documents\Test\Changes.doc The document
> name is not relevant, as long as you define it in the macro. Put the text to
> be found in the first column and the replacement in the second column and
> run the macro with the document to be edited active.
>
> Sub ReplaceFromTableList()
> Dim oChanges As Document, oDoc As Document
> Dim oTable As Table
> Dim oRng As Range
> Dim rFindText As Range, rReplacement As Range
> Dim i As Long
> Dim sFname As String
> sFname = "D:\My Documents\Test\Changes.doc"
> Set oDoc = ActiveDocument
> Set oRng = oDoc.Range
> Set oChanges = Documents.Open(sFname)
> Set oTable = oChanges.Tables(1)
> oDoc.Activate
> For i = 1 To oTable.Rows.Count
> Set rFindText = oTable.Cell(i, 1).Range
> rFindText.End = rFindText.End - 1
> Set rReplacement = oTable.Cell(i, 2).Range
> rReplacement.End = rReplacement.End - 1
> With oRng.Find
> .ClearFormatting
> .Replacement.ClearFormatting
> Do While .Execute(findText:=rFindText, _
> MatchWholeWord:=True, _
> MatchWildcards:=False, _
> Forward:=True, _
> Wrap:=wdFindContinue) = True
> oRng.Text = rReplacement
> Loop
> End With
> Next i
> oChanges.Close wdDoNotSaveChanges
> End Sub
>
>
> --
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Graham Mayor - Word MVP
>
> My web site www.gmayor.com
> Word MVP web site http://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>
>
> "LaRana6261" <LaRana6261(a)discussions.microsoft.com> wrote in message
> news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8(a)microsoft.com...
> > Hello,
> > I need a macro for a .doc that will find certain text and replace it with
> > its corresponding value from a table of values.
> > Examples:
> > Text = s[32] cross reference "Record Name"
> > Text =s[33] cross reference "Record Address"
> >
> > My table has about 190 record names which I need to replace everytime the
> > field "Code" is found. In the example above, s[32] will be replace by
> > "record
> > name". can someone assist?
> >
> > Thanks,
> > --
> > EdV
>
>
> .
>
From: Graham Mayor on
You are welcome :)

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

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

"LaRana6261" <LaRana6261(a)discussions.microsoft.com> wrote in message
news:23181512-12E9-4440-B3A4-CED562DF83E2(a)microsoft.com...
> This is great! I can work with this!
>
> Thank you,
> --
> EdV
>
>
> "Graham Mayor" wrote:
>
>> The following should work. It uses a two column table, without blank
>> rows,
>> here saved as a document - D:\My Documents\Test\Changes.doc The document
>> name is not relevant, as long as you define it in the macro. Put the text
>> to
>> be found in the first column and the replacement in the second column and
>> run the macro with the document to be edited active.
>>
>> Sub ReplaceFromTableList()
>> Dim oChanges As Document, oDoc As Document
>> Dim oTable As Table
>> Dim oRng As Range
>> Dim rFindText As Range, rReplacement As Range
>> Dim i As Long
>> Dim sFname As String
>> sFname = "D:\My Documents\Test\Changes.doc"
>> Set oDoc = ActiveDocument
>> Set oRng = oDoc.Range
>> Set oChanges = Documents.Open(sFname)
>> Set oTable = oChanges.Tables(1)
>> oDoc.Activate
>> For i = 1 To oTable.Rows.Count
>> Set rFindText = oTable.Cell(i, 1).Range
>> rFindText.End = rFindText.End - 1
>> Set rReplacement = oTable.Cell(i, 2).Range
>> rReplacement.End = rReplacement.End - 1
>> With oRng.Find
>> .ClearFormatting
>> .Replacement.ClearFormatting
>> Do While .Execute(findText:=rFindText, _
>> MatchWholeWord:=True, _
>> MatchWildcards:=False, _
>> Forward:=True, _
>> Wrap:=wdFindContinue) = True
>> oRng.Text = rReplacement
>> Loop
>> End With
>> Next i
>> oChanges.Close wdDoNotSaveChanges
>> End Sub
>>
>>
>> --
>> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>> Graham Mayor - Word MVP
>>
>> My web site www.gmayor.com
>> Word MVP web site http://word.mvps.org
>> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>>
>>
>> "LaRana6261" <LaRana6261(a)discussions.microsoft.com> wrote in message
>> news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8(a)microsoft.com...
>> > Hello,
>> > I need a macro for a .doc that will find certain text and replace it
>> > with
>> > its corresponding value from a table of values.
>> > Examples:
>> > Text = s[32] cross reference "Record Name"
>> > Text =s[33] cross reference "Record Address"
>> >
>> > My table has about 190 record names which I need to replace everytime
>> > the
>> > field "Code" is found. In the example above, s[32] will be replace by
>> > "record
>> > name". can someone assist?
>> >
>> > Thanks,
>> > --
>> > EdV
>>
>>
>> .
>>


From: LaRana6261 on
Spoke too soon...

I am finding some issues where my table has the following values

L[1 STATE [1]
L[10 PROP STREET SUFFIX [10]

So when the macros looks for "L[10" it returns "STATE[1]". In other words,
it is not looking for an exact match as it is set by the macro
(MatchWholeWord:=True).
can this be fixed?

Also, how can I make the results in 'BOLD'

thanks,

--
EdV


"Graham Mayor" wrote:

> You are welcome :)
>
> --
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Graham Mayor - Word MVP
>
> My web site www.gmayor.com
> Word MVP web site http://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>
> "LaRana6261" <LaRana6261(a)discussions.microsoft.com> wrote in message
> news:23181512-12E9-4440-B3A4-CED562DF83E2(a)microsoft.com...
> > This is great! I can work with this!
> >
> > Thank you,
> > --
> > EdV
> >
> >
> > "Graham Mayor" wrote:
> >
> >> The following should work. It uses a two column table, without blank
> >> rows,
> >> here saved as a document - D:\My Documents\Test\Changes.doc The document
> >> name is not relevant, as long as you define it in the macro. Put the text
> >> to
> >> be found in the first column and the replacement in the second column and
> >> run the macro with the document to be edited active.
> >>
> >> Sub ReplaceFromTableList()
> >> Dim oChanges As Document, oDoc As Document
> >> Dim oTable As Table
> >> Dim oRng As Range
> >> Dim rFindText As Range, rReplacement As Range
> >> Dim i As Long
> >> Dim sFname As String
> >> sFname = "D:\My Documents\Test\Changes.doc"
> >> Set oDoc = ActiveDocument
> >> Set oRng = oDoc.Range
> >> Set oChanges = Documents.Open(sFname)
> >> Set oTable = oChanges.Tables(1)
> >> oDoc.Activate
> >> For i = 1 To oTable.Rows.Count
> >> Set rFindText = oTable.Cell(i, 1).Range
> >> rFindText.End = rFindText.End - 1
> >> Set rReplacement = oTable.Cell(i, 2).Range
> >> rReplacement.End = rReplacement.End - 1
> >> With oRng.Find
> >> .ClearFormatting
> >> .Replacement.ClearFormatting
> >> Do While .Execute(findText:=rFindText, _
> >> MatchWholeWord:=True, _
> >> MatchWildcards:=False, _
> >> Forward:=True, _
> >> Wrap:=wdFindContinue) = True
> >> oRng.Text = rReplacement
> >> Loop
> >> End With
> >> Next i
> >> oChanges.Close wdDoNotSaveChanges
> >> End Sub
> >>
> >>
> >> --
> >> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> >> Graham Mayor - Word MVP
> >>
> >> My web site www.gmayor.com
> >> Word MVP web site http://word.mvps.org
> >> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> >>
> >>
> >> "LaRana6261" <LaRana6261(a)discussions.microsoft.com> wrote in message
> >> news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8(a)microsoft.com...
> >> > Hello,
> >> > I need a macro for a .doc that will find certain text and replace it
> >> > with
> >> > its corresponding value from a table of values.
> >> > Examples:
> >> > Text = s[32] cross reference "Record Name"
> >> > Text =s[33] cross reference "Record Address"
> >> >
> >> > My table has about 190 record names which I need to replace everytime
> >> > the
> >> > field "Code" is found. In the example above, s[32] will be replace by
> >> > "record
> >> > name". can someone assist?
> >> >
> >> > Thanks,
> >> > --
> >> > EdV
> >>
> >>
> >> .
> >>
>
>
> .
>