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
|
|
0
|
|
|
|
Reply
|
Utf
|
5/11/2010 9:03:01 PM |
|
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@discussions.microsoft.com> wrote in message
news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8@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
|
|
0
|
|
|
|
Reply
|
Graham
|
5/12/2010 5:03:22 AM
|
|
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@discussions.microsoft.com> wrote in message
> news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8@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
>
>
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
5/13/2010 12:14:01 AM
|
|
You are welcome :)
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
"LaRana6261" <LaRana6261@discussions.microsoft.com> wrote in message
news:23181512-12E9-4440-B3A4-CED562DF83E2@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@discussions.microsoft.com> wrote in message
>> news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8@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
>>
>>
>> .
>>
|
|
0
|
|
|
|
Reply
|
Graham
|
5/13/2010 4:31:34 AM
|
|
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@discussions.microsoft.com> wrote in message
> news:23181512-12E9-4440-B3A4-CED562DF83E2@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@discussions.microsoft.com> wrote in message
> >> news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8@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
> >>
> >>
> >> .
> >>
>
>
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
5/20/2010 6:47:01 PM
|
|
The bold part is easy . Immediately after the line
oRng.Text = rReplacement
add the line
oRng.Bold = True
The other part I am a little unclear about.
The macro finds the two strings correctly in my test document. However as
the macro tests each string separately from start to finish if you then
search for sat L[1 it will find that in both the strings, if the strings are
unchanged by the earlier replacement. You need to set your table so that
subsequent searches do not clash with those that have already gone.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
"LaRana6261" <LaRana6261@discussions.microsoft.com> wrote in message
news:B5278CD8-02FE-48B3-A302-8C9D3D14B863@microsoft.com...
> 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@discussions.microsoft.com> wrote in message
>> news:23181512-12E9-4440-B3A4-CED562DF83E2@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@discussions.microsoft.com> wrote in message
>> >> news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8@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
>> >>
>> >>
>> >> .
>> >>
>>
>>
>> .
>>
|
|
0
|
|
|
|
Reply
|
Graham
|
5/21/2010 6:02:07 AM
|
|
Thanks, ok the bold thing I almost had correctly... I'll fix.
On the other glitch, well "L[1" may repeat itself later in the document
(just like "L[10" ).
I wanted to make sure that the macro only looks for "L[1" event if it finds
it in "L[10" which is causing undesirable results. I guess I can manipulate
each 10th with a separate "prefix", but that may create confusion for the
user.
I also found that if I remove the "[" to say "L1" instead of "L[1" then Word
does find exact matches and does not confuse it with 'L10'. I may go this
way instead...
In any event, your macro is already saving me hundreds of keystrokes and
countless hours of work!
I appreciate it.
--
EdV
"Graham Mayor" wrote:
> The bold part is easy . Immediately after the line
> oRng.Text = rReplacement
> add the line
> oRng.Bold = True
> The other part I am a little unclear about.
> The macro finds the two strings correctly in my test document. However as
> the macro tests each string separately from start to finish if you then
> search for sat L[1 it will find that in both the strings, if the strings are
> unchanged by the earlier replacement. You need to set your table so that
> subsequent searches do not clash with those that have already gone.
>
> --
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Graham Mayor - Word MVP
>
> My web site www.gmayor.com
> Word MVP web site http://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>
>
>
> "LaRana6261" <LaRana6261@discussions.microsoft.com> wrote in message
> news:B5278CD8-02FE-48B3-A302-8C9D3D14B863@microsoft.com...
> > 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@discussions.microsoft.com> wrote in message
> >> news:23181512-12E9-4440-B3A4-CED562DF83E2@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@discussions.microsoft.com> wrote in message
> >> >> news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8@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
> >> >>
> >> >>
> >> >> .
> >> >>
> >>
> >>
> >> .
> >>
>
>
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
5/21/2010 4:47:01 PM
|
|
If you want to be more specific in your searches, then you will need to
consider using wildcard strings (noting that in a wildcard string square
brackets have a particular significance and will need to be handled
correctly). You will need to change the section of the macro
Do While .Execute(findText:=rFindText, _
MatchWholeWord:=True, _
MatchWildcards:=False, _
Forward:=True, _
Wrap:=wdFindContinue) = True
to
Do While .Execute(findText:=rFindText, _
MatchWildcards:=True, _
Forward:=True, _
Wrap:=wdFindContinue) = True
You could then search for (say) L\[1[ ]
which will find "L[1 " but not the first part of "L[10"
See http://www.gmayor.com/replace_using_wildcards.htm
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP
My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
"LaRana6261" <LaRana6261@discussions.microsoft.com> wrote in message
news:D2E877B1-D481-4D99-82EF-B00FD3E560A9@microsoft.com...
> Thanks, ok the bold thing I almost had correctly... I'll fix.
>
> On the other glitch, well "L[1" may repeat itself later in the document
> (just like "L[10" ).
> I wanted to make sure that the macro only looks for "L[1" event if it
> finds
> it in "L[10" which is causing undesirable results. I guess I can
> manipulate
> each 10th with a separate "prefix", but that may create confusion for the
> user.
>
> I also found that if I remove the "[" to say "L1" instead of "L[1" then
> Word
> does find exact matches and does not confuse it with 'L10'. I may go this
> way instead...
>
> In any event, your macro is already saving me hundreds of keystrokes and
> countless hours of work!
>
> I appreciate it.
>
>
>
>
>
>
> --
> EdV
>
>
> "Graham Mayor" wrote:
>
>> The bold part is easy . Immediately after the line
>> oRng.Text = rReplacement
>> add the line
>> oRng.Bold = True
>> The other part I am a little unclear about.
>> The macro finds the two strings correctly in my test document. However as
>> the macro tests each string separately from start to finish if you then
>> search for sat L[1 it will find that in both the strings, if the strings
>> are
>> unchanged by the earlier replacement. You need to set your table so that
>> subsequent searches do not clash with those that have already gone.
>>
>> --
>> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>> Graham Mayor - Word MVP
>>
>> My web site www.gmayor.com
>> Word MVP web site http://word.mvps.org
>> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>>
>>
>>
>> "LaRana6261" <LaRana6261@discussions.microsoft.com> wrote in message
>> news:B5278CD8-02FE-48B3-A302-8C9D3D14B863@microsoft.com...
>> > 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@discussions.microsoft.com> wrote in message
>> >> news:23181512-12E9-4440-B3A4-CED562DF83E2@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@discussions.microsoft.com> wrote in
>> >> >> message
>> >> >> news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8@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
>> >> >>
>> >> >>
>> >> >> .
>> >> >>
>> >>
>> >>
>> >> .
>> >>
>>
>>
>> .
>>
|
|
0
|
|
|
|
Reply
|
Graham
|
5/22/2010 6:00:10 AM
|
|
Thanks!
--
EdV
"Graham Mayor" wrote:
> If you want to be more specific in your searches, then you will need to
> consider using wildcard strings (noting that in a wildcard string square
> brackets have a particular significance and will need to be handled
> correctly). You will need to change the section of the macro
>
> Do While .Execute(findText:=rFindText, _
> MatchWholeWord:=True, _
> MatchWildcards:=False, _
> Forward:=True, _
> Wrap:=wdFindContinue) = True
> to
>
> Do While .Execute(findText:=rFindText, _
> MatchWildcards:=True, _
> Forward:=True, _
> Wrap:=wdFindContinue) = True
>
> You could then search for (say) L\[1[ ]
> which will find "L[1 " but not the first part of "L[10"
>
> See http://www.gmayor.com/replace_using_wildcards.htm
>
>
> --
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> Graham Mayor - Word MVP
>
> My web site www.gmayor.com
> Word MVP web site http://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>
> "LaRana6261" <LaRana6261@discussions.microsoft.com> wrote in message
> news:D2E877B1-D481-4D99-82EF-B00FD3E560A9@microsoft.com...
> > Thanks, ok the bold thing I almost had correctly... I'll fix.
> >
> > On the other glitch, well "L[1" may repeat itself later in the document
> > (just like "L[10" ).
> > I wanted to make sure that the macro only looks for "L[1" event if it
> > finds
> > it in "L[10" which is causing undesirable results. I guess I can
> > manipulate
> > each 10th with a separate "prefix", but that may create confusion for the
> > user.
> >
> > I also found that if I remove the "[" to say "L1" instead of "L[1" then
> > Word
> > does find exact matches and does not confuse it with 'L10'. I may go this
> > way instead...
> >
> > In any event, your macro is already saving me hundreds of keystrokes and
> > countless hours of work!
> >
> > I appreciate it.
> >
> >
> >
> >
> >
> >
> > --
> > EdV
> >
> >
> > "Graham Mayor" wrote:
> >
> >> The bold part is easy . Immediately after the line
> >> oRng.Text = rReplacement
> >> add the line
> >> oRng.Bold = True
> >> The other part I am a little unclear about.
> >> The macro finds the two strings correctly in my test document. However as
> >> the macro tests each string separately from start to finish if you then
> >> search for sat L[1 it will find that in both the strings, if the strings
> >> are
> >> unchanged by the earlier replacement. You need to set your table so that
> >> subsequent searches do not clash with those that have already gone.
> >>
> >> --
> >> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> >> Graham Mayor - Word MVP
> >>
> >> My web site www.gmayor.com
> >> Word MVP web site http://word.mvps.org
> >> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
> >>
> >>
> >>
> >> "LaRana6261" <LaRana6261@discussions.microsoft.com> wrote in message
> >> news:B5278CD8-02FE-48B3-A302-8C9D3D14B863@microsoft.com...
> >> > 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@discussions.microsoft.com> wrote in message
> >> >> news:23181512-12E9-4440-B3A4-CED562DF83E2@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@discussions.microsoft.com> wrote in
> >> >> >> message
> >> >> >> news:EB9D02B7-14AE-4A03-B79F-5E56C583B3D8@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
> >> >> >>
> >> >> >>
> >> >> >> .
> >> >> >>
> >> >>
> >> >>
> >> >> .
> >> >>
> >>
> >>
> >> .
> >>
>
>
> .
>
|
|
0
|
|
|
|
Reply
|
Utf
|
5/27/2010 8:37:56 PM
|
|
|
8 Replies
404 Views
(page loaded in 0.412 seconds)
|