MS Office Forum / Word / Programming / July 2005
How to bold a list of words by the fastest method.
|
|
Thread rating:  |
Emily - 16 Jul 2005 18:08 GMT Hello,
Using Word97.
I have a list of words (say, 10 at most) in an array that are to be set with a bold font wherever they appear in a document. The document can be a very long one - perhaps 200 pages, so I'd like to use the fastest mehod to do this.
I've seen various methods in a Google search, and it seems that using the Find object to replace the font with bold seems to be the fastest, but I'm not sure how to loop through an array and using Find/Replace to change the font to "Bold" for each of the words in the array.
Could someone please suggest some code for this?
TIA!
Helmut Weber - 16 Jul 2005 20:02 GMT Hi Emily,
I found this slightly faster then replacing, with the doc containg each of the 10 array-words 455 times.
Sub Test3653() Dim sArr(9) As String Dim rDcm As Range Dim sTim As Single Dim l As Long ResetSearch For l = 0 To 9 ' fill array sArr(l) = "Word" & Format(l, "-000") Next sTim = Time For l = 0 To 9 Set rDcm = ActiveDocument.Range With rDcm.Find .Text = sArr(l) While .Execute rDcm.Font.Bold = True Wend ActiveDocument.Save End With Next MsgBox Time - sTim ResetSearch End Sub
Greetings from Bavaria, Germany
Helmut Weber, MVP, WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Helmut Weber - 16 Jul 2005 20:17 GMT hmm..., forgot about this part:
Public Sub ResetSearch() With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False ' plus some more if needed .Execute End With End Sub
Greetings from Bavaria, Germany
Helmut Weber, MVP, WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Emily - 16 Jul 2005 21:07 GMT Thank you very much for the code, Helmut.
Could you tell mew please ... when do you run Sub ResetSearch, and what is it's purpose?
I'm guessing that it somehow clears all the parameters in the Find object. Is that correct? Is that something one should use routinely berfore using the Find object?
Rgds,
Em
>hmm..., forgot about this part: > [quoted text clipped - 23 lines] >Win XP, Office 2003 >"red.sys" & Chr$(64) & "t-online.de" Helmut Weber - 16 Jul 2005 22:29 GMT Hi Emily,
Yes, indeed.
The very same routine is to be found under different names.
Greetings from Bavaria, Germany
Helmut Weber, MVP, WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
Emily - 16 Jul 2005 23:37 GMT Thanks again Helmut ... I've copied your code into my template, and will be working with it a little later.
Rgds,
Em
>Hi Emily, > [quoted text clipped - 8 lines] >Win XP, Office 2003 >"red.sys" & Chr$(64) & "t-online.de" Klaus Linke - 18 Jul 2005 18:36 GMT Hi Helmut, Emily,
If you're very sure you have a good backup, a thrown-in "ActiveDocument.UndoClear" can speed up such multiple replacements a lot, too.
Regards, Klaus
> Hi Emily, > [quoted text clipped - 31 lines] > Win XP, Office 2003 > "red.sys" & Chr$(64) & "t-online.de" Klaus Linke - 18 Jul 2005 18:46 GMT > If you're very sure you have a good backup, a thrown-in > "ActiveDocument.UndoClear" can speed up such multiple replacements a lot, > too. I just tried, and didn't see much of a difference. But I do if I change your macro a bit:
Sub Test3653() Dim sArr(9) As String Dim rDcm As Range Dim sTim As Single Dim l As Long ResetSearch For l = 0 To 9 ' fill array sArr(l) = "Word" & Format(l, "-000") Next sTim = Time For l = 0 To 9 Set rDcm = ActiveDocument.Range With rDcm.Find .Text = sArr(l) .Replacement.Font.Bold = False .Execute Replace:=wdReplaceAll ActiveDocument.UndoClear ActiveDocument.Save ' might be commented out? End With Next MsgBox Time - sTim ResetSearch End Sub
Klaus
Helmut Weber - 19 Jul 2005 19:28 GMT Hi Klaus,
as I know You enjoy brain twisters, this time about the fastest method of bolding. Have you compared "replace" vs. "while .execute"?
As I said, I found "while .execute" slightly faster then "replace", and more than that, when repeating the same routine, "replace" gave different results according to time, from x seconds to 6*x seconds, whereas the reaction time of "while .execute" was usually 0.8*x.
Well. I wonder...
Gruss aus Landsberg am Lech
Helmut Weber, MVP, WordVBA
Win XP, Office 2003 "red.sys" & Chr$(64) & "t-online.de"
|
|
|