MS Office Forum / Word / Programming / August 2007
Having a problem with a Do/Loop replace macro...
|
|
Thread rating:  |
adimax@gmail.com - 09 Aug 2007 18:46 GMT 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.
adimax@gmail.com - 09 Aug 2007 18:54 GMT On Aug 9, 1:46 pm, adi...@gmail.com 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 [quoted text clipped - 16 lines] > > Thanks in advance. Forgot to mention: Windows XP, Office 2003
adimax@gmail.com - 09 Aug 2007 19:11 GMT On Aug 9, 1:46 pm, adi...@gmail.com 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 [quoted text clipped - 16 lines] > > 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.
Doug Robbins - Word MVP - 09 Aug 2007 20:50 GMT 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.
 Signature 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, adi...@gmail.com wrote: >> I'm trying to execute this macro on hourly raw data dumps and I'm [quoted text clipped - 49 lines] > Either way, anyone think the code could be better/cleaner? Input it > still very welcome. adimax@gmail.com - 09 Aug 2007 21:56 GMT 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" <d...@REMOVECAPSmvps.org> wrote:
> You need to use the Wrap: attribute > [quoted text clipped - 80 lines] > > Either way, anyone think the code could be better/cleaner? Input it > > still very welcome. adimax@gmail.com - 09 Aug 2007 21:56 GMT 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" <d...@REMOVECAPSmvps.org> wrote:
> You need to use the Wrap: attribute > [quoted text clipped - 80 lines] > > Either way, anyone think the code could be better/cleaner? Input it > > still very welcome. Doug Robbins - Word MVP - 10 Aug 2007 02:33 GMT 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
 Signature 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, > [quoted text clipped - 106 lines] >> > Either way, anyone think the code could be better/cleaner? Input it >> > still very welcome. adimax@gmail.com - 10 Aug 2007 22:40 GMT 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!
On Aug 9, 9:33 pm, "Doug Robbins - Word MVP" <d...@REMOVECAPSmvps.org> wrote:
> Use > [quoted text clipped - 128 lines] > >> > Either way, anyone think the code could be better/cleaner? Input it > >> > still very welcome. Doug Robbins - Word MVP - 11 Aug 2007 09:43 GMT \/[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
 Signature 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, > [quoted text clipped - 161 lines] >> >> > Either way, anyone think the code could be better/cleaner? Input it >> >> > still very welcome. adimax@gmail.com - 12 Aug 2007 02:51 GMT Doug,
I totally forgot about using an array. It works perfectly, and you are amazing. Thanks so much for your support!
Benjamin
On Aug 11, 4:43 am, "Doug Robbins - Word MVP" <d...@REMOVECAPSmvps.org> wrote:
> \/[0-9]{3,} will find only numbers >99 that are preceded by a \. > [quoted text clipped - 202 lines] > >> >> > Either way, anyone think the code could be better/cleaner? Input it > >> >> > still very welcome.
|
|
|