Having a problem with a Do/Loop replace macro...

A

adimax

I'm trying to execute this macro on hourly raw data dumps and I'm
finding that when it gets to the end of the document it goes back up
it and seems to be undoing the bold/font color changes. Am I missing
something that forces the macro to stop at the end of the document...
I assumed it would stop when it found no more changes at the end...

Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="/6346") = True
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.Color = wdColorRed
Selection.EndKey Unit:=wdLine
Loop
End With
End Sub

Thanks in advance.
 
A

adimax

I'm trying to execute this macro on hourly raw data dumps and I'm
finding that when it gets to the end of the document it goes back up
it and seems to be undoing the bold/font color changes. Am I missing
something that forces the macro to stop at the end of the document...
I assumed it would stop when it found no more changes at the end...

Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="/6346") = True
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.Color = wdColorRed
Selection.EndKey Unit:=wdLine
Loop
End With
End Sub

Thanks in advance.

Forgot to mention:
Windows XP, Office 2003
 
A

adimax

I'm trying to execute this macro on hourly raw data dumps and I'm
finding that when it gets to the end of the document it goes back up
it and seems to be undoing the bold/font color changes. Am I missing
something that forces the macro to stop at the end of the document...
I assumed it would stop when it found no more changes at the end...

Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="/6346") = True
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.Color = wdColorRed
Selection.EndKey Unit:=wdLine
Loop
End With
End Sub

Thanks in advance.

Actually, this seems to be working for me:

Selection.HomeKey unit:=wdStory
With Selection.Find
.ClearFormatting
.Text = "/6346"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False

Do While .Execute
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.Color = wdColorRed
Selection.EndKey unit:=wdLine
Loop
End With
End Sub

Although Word is now constantly popping up a Debugger box asking me to
Continue, Stop, Debug, etc... but thats happening on all my Macros
now.

Either way, anyone think the code could be better/cleaner? Input it
still very welcome.
 
D

Doug Robbins - Word MVP

You need to use the Wrap: attribute

Dim myrange as Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="/6346", Forward:=True, Wrap:=wdFindStop) =
True
Set myrange = Selection.Bookmarks("\line").Range
With myrange.Font
.Bold = wdToggle
.Color = wdColorRed
End With
Selection.Collapse wdCollapseEnd
Loop
End With

Note, that if you change the text to Bold, it may flow over to the next
line.
--
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
 
A

adimax

Doug,

Thanks sooo much for that piece of code. Its so much faster than the
way I was doing it, on this thin client at least (which is what the
majority of the users will be on).

One additional query if I might...

Without repeating the following line over and over (essentially copy/
past spamming the code)
Do While .Execute(findText:="/6346", Forward:=True, Wrap:=wdFindStop)
=

How can I get the

findText:=

to allow multiple strings. Like /6346, /137, and /138? I have about 30
strings I want to color red, another 10 or so blue, and the rest
green. But every time I try and add a second string it debugs on me.

Again, thanks for the help so far!
 
A

adimax

Doug,

Thanks sooo much for that piece of code. Its so much faster than the
way I was doing it, on this thin client at least (which is what the
majority of the users will be on).

One additional query if I might...

Without repeating the following line over and over (essentially copy/
past spamming the code)
Do While .Execute(findText:="/6346", Forward:=True, Wrap:=wdFindStop)
=

How can I get the

findText:=

to allow multiple strings. Like /6346, /137, and /138? I have about 30
strings I want to color red, another 10 or so blue, and the rest
green. But every time I try and add a second string it debugs on me.

Again, thanks for the help so far!
 
D

Doug Robbins - Word MVP

Use

Do While .Execute(findText:="\/[0-9]{3,}", MatchWildCards:=True,
Forward:=True, Wrap:=wdFindStop)

For an explanation, see the article "Finding and replacing characters using
wildcards" at:

http://www.word.mvps.org/FAQs/General/UsingWildcards.htm


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

adimax

Doug,

Running into some problems trying to use the WildCards feature. I have
read over the site you linked below, but I'm still missing something.
So far I have tried:

1. Do While .Execute(findText:="\/[0-9]{3,}",
This works but bolds obviously A LOT of data on the screen because its
looking for any # 3 in a row, right?

Thinking I understood it, I then tried:

2. Do While .Execute(findText:="\/<137><138>",

That failed. I also tried (137)(138) and [137][138] (with and without
the \/ beginning), but no matter the combination I couldn't get it to
find any of the range of data I am looking for.

Any ideas? I essentially want it to do the Find (and later Replace)
functions on a large # of patterns: /80, /21, /23, /137, /138, /6346,
etc.

Thanks!

Use

Do While .Execute(findText:="\/[0-9]{3,}", MatchWildCards:=True,
Forward:=True, Wrap:=wdFindStop)

For an explanation, see the article "Finding and replacing characters using
wildcards" at:

http://www.word.mvps.org/FAQs/General/UsingWildcards.htm

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


Thanks sooo much for that piece of code. Its so much faster than the
way I was doing it, on this thin client at least (which is what the
majority of the users will be on).
One additional query if I might...
Without repeating the following line over and over (essentially copy/
past spamming the code)
Do While .Execute(findText:="/6346", Forward:=True, Wrap:=wdFindStop)
=
How can I get the

to allow multiple strings. Like /6346, /137, and /138? I have about 30
strings I want to color red, another 10 or so blue, and the rest
green. But every time I try and add a second string it debugs on me.
Again, thanks for the help so far!
 
D

Doug Robbins - Word MVP

\/[0-9]{3,} will find only numbers >99 that are preceded by a \.

However, maybe you will need to put the numeric strings that you are
searching for in an array and then have the macro step through the array to
find each one in turn.

Here is an example using the three numeric strings that you mentioned:

Dim vFindText As Variant
Dim myrange As Range
Dim i As Long

vFindText = Array("/6346", "/137", "/138")
For i = LBound(vFindText) To UBound(vFindText)
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:=vFindText(i), Forward:=True,
MatchWildcards:=False, Wrap:=wdFindStop) = True
Set myrange = Selection.Bookmarks("\line").Range
With myrange.Font
.Bold = wdToggle
.Color = wdColorRed
End With
Selection.Collapse wdCollapseEnd
Loop
End With
Next i

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

Doug,

Running into some problems trying to use the WildCards feature. I have
read over the site you linked below, but I'm still missing something.
So far I have tried:

1. Do While .Execute(findText:="\/[0-9]{3,}",
This works but bolds obviously A LOT of data on the screen because its
looking for any # 3 in a row, right?

Thinking I understood it, I then tried:

2. Do While .Execute(findText:="\/<137><138>",

That failed. I also tried (137)(138) and [137][138] (with and without
the \/ beginning), but no matter the combination I couldn't get it to
find any of the range of data I am looking for.

Any ideas? I essentially want it to do the Find (and later Replace)
functions on a large # of patterns: /80, /21, /23, /137, /138, /6346,
etc.

Thanks!

Use

Do While .Execute(findText:="\/[0-9]{3,}", MatchWildCards:=True,
Forward:=True, Wrap:=wdFindStop)

For an explanation, see the article "Finding and replacing characters
using
wildcards" at:

http://www.word.mvps.org/FAQs/General/UsingWildcards.htm

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


Thanks sooo much for that piece of code. Its so much faster than the
way I was doing it, on this thin client at least (which is what the
majority of the users will be on).
One additional query if I might...
Without repeating the following line over and over (essentially copy/
past spamming the code)
Do While .Execute(findText:="/6346", Forward:=True, Wrap:=wdFindStop)
=
How can I get the

to allow multiple strings. Like /6346, /137, and /138? I have about 30
strings I want to color red, another 10 or so blue, and the rest
green. But every time I try and add a second string it debugs on me.
Again, thanks for the help so far!
On Aug 9, 3:50 pm, "Doug Robbins - Word MVP" <[email protected]>
wrote:
You need to use the Wrap: attribute
Dim myrange as Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="/6346", Forward:=True,
Wrap:=wdFindStop)
=
True
Set myrange = Selection.Bookmarks("\line").Range
With myrange.Font
.Bold = wdToggle
.Color = wdColorRed
End With
Selection.Collapse wdCollapseEnd
Loop
End With
Note, that if you change the text to Bold, it may flow over to the
next
line.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP
On Aug 9, 1:46 pm, (e-mail address removed) wrote:
I'm trying to execute this macro on hourly raw data dumps and I'm
finding that when it gets to the end of the document it goes back
up
it and seems to be undoing the bold/font color changes. Am I
missing
something that forces the macro to stop at the end of the
document...
I assumed it would stop when it found no more changes at the end...
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="/6346") = True
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.Color = wdColorRed
Selection.EndKey Unit:=wdLine
Loop
End With
End Sub
Thanks in advance.
Actually, this seems to be working for me:
Selection.HomeKey unit:=wdStory
With Selection.Find
.ClearFormatting
.Text = "/6346"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.Color = wdColorRed
Selection.EndKey unit:=wdLine
Loop
End With
End Sub
Although Word is now constantly popping up a Debugger box asking me
to
Continue, Stop, Debug, etc... but thats happening on all my Macros
now.
Either way, anyone think the code could be better/cleaner? Input it
still very welcome.
 
A

adimax

Doug,

I totally forgot about using an array. It works perfectly, and you are
amazing. Thanks so much for your support!

Benjamin

\/[0-9]{3,} will find only numbers >99 that are preceded by a \.

However, maybe you will need to put the numeric strings that you are
searching for in an array and then have the macro step through the array to
find each one in turn.

Here is an example using the three numeric strings that you mentioned:

Dim vFindText As Variant
Dim myrange As Range
Dim i As Long

vFindText = Array("/6346", "/137", "/138")
For i = LBound(vFindText) To UBound(vFindText)
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:=vFindText(i), Forward:=True,
MatchWildcards:=False, Wrap:=wdFindStop) = True
Set myrange = Selection.Bookmarks("\line").Range
With myrange.Font
.Bold = wdToggle
.Color = wdColorRed
End With
Selection.Collapse wdCollapseEnd
Loop
End With
Next i

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


Running into some problems trying to use the WildCards feature. I have
read over the site you linked below, but I'm still missing something.
So far I have tried:
1. Do While .Execute(findText:="\/[0-9]{3,}",
This works but bolds obviously A LOT of data on the screen because its
looking for any # 3 in a row, right?
Thinking I understood it, I then tried:
2. Do While .Execute(findText:="\/<137><138>",
That failed. I also tried (137)(138) and [137][138] (with and without
the \/ beginning), but no matter the combination I couldn't get it to
find any of the range of data I am looking for.
Any ideas? I essentially want it to do the Find (and later Replace)
functions on a large # of patterns: /80, /21, /23, /137, /138, /6346,
etc.

Use
Do While .Execute(findText:="\/[0-9]{3,}", MatchWildCards:=True,
Forward:=True, Wrap:=wdFindStop)
For an explanation, see the article "Finding and replacing characters
using
wildcards" at:
http://www.word.mvps.org/FAQs/General/UsingWildcards.htm
--
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

Doug,
Thanks sooo much for that piece of code. Its so much faster than the
way I was doing it, on this thin client at least (which is what the
majority of the users will be on).
One additional query if I might...
Without repeating the following line over and over (essentially copy/
past spamming the code)
Do While .Execute(findText:="/6346", Forward:=True, Wrap:=wdFindStop)
=
How can I get the
findText:=
to allow multiple strings. Like /6346, /137, and /138? I have about 30
strings I want to color red, another 10 or so blue, and the rest
green. But every time I try and add a second string it debugs on me.
Again, thanks for the help so far!
On Aug 9, 3:50 pm, "Doug Robbins - Word MVP" <[email protected]>
wrote:
You need to use the Wrap: attribute
Dim myrange as Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="/6346", Forward:=True,
Wrap:=wdFindStop)
=
True
Set myrange = Selection.Bookmarks("\line").Range
With myrange.Font
.Bold = wdToggle
.Color = wdColorRed
End With
Selection.Collapse wdCollapseEnd
Loop
End With
Note, that if you change the text to Bold, it may flow over to the
next
line.
--
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

On Aug 9, 1:46 pm, (e-mail address removed) wrote:
I'm trying to execute this macro on hourly raw data dumps and I'm
finding that when it gets to the end of the document it goes back
up
it and seems to be undoing the bold/font color changes. Am I
missing
something that forces the macro to stop at the end of the
document...
I assumed it would stop when it found no more changes at the end...
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findText:="/6346") = True
Selection.Find.Execute
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.Color = wdColorRed
Selection.EndKey Unit:=wdLine
Loop
End With
End Sub
Thanks in advance.
Actually, this seems to be working for me:
Selection.HomeKey unit:=wdStory
With Selection.Find
.ClearFormatting
.Text = "/6346"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute
Selection.HomeKey unit:=wdLine
Selection.EndKey unit:=wdLine, Extend:=wdExtend
Selection.Font.Bold = wdToggle
Selection.Font.Color = wdColorRed
Selection.EndKey unit:=wdLine
Loop
End With
End Sub
Although Word is now constantly popping up a Debugger box asking me
to
Continue, Stop, Debug, etc... but thats happening on all my Macros
now.
Either way, anyone think the code could be better/cleaner? Input it
still very welcome.
 

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