How to bold a list of words by the fastest method.

E

Emily

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

Helmut Weber

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

Helmut Weber

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

Emily

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
 
H

Helmut Weber

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

Emily

Thanks again Helmut ... I've copied your code into my template, and
will be working with it a little later.

Rgds,

Em
 
K

Klaus Linke

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
 
K

Klaus Linke

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
 
H

Helmut Weber

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"
 

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