![]() |
|
#7
|
|||
|
|||
|
Chopper,
You don't have to replace text if you find it. You just want to do something with that text when found. I think eduzs point, and mine, to find the text wherever it is in the thousands of paragraphs and process it. For example, I created as document with 10,000 paragraphs of similar text then I went in and added "XXX" to about a dozen or so. See these examples: Code:
Sub Example1()
'Burtally slow. Will probably lock up Word.
Dim oPar As Paragraph
Dim lngIndex As Long
Dim lngCount As Long
For lngIndex = 1 To ActiveDocument.Paragraphs.Count
Set oPar = ActiveDocument.Paragraphs(lngIndex)
If InStr(oPar.Range.Text, "XXX") > 0 Then
lngCount = lngCount + 1
DoEvents
'Do something with found text
End If
Next
MsgBox lngCount
lbl_Exit:
Exit Sub
End Sub
Sub Example2()
'Faster but still slow
Dim oPar As Paragraph
Dim lngCount As Long
For Each oPar In ActiveDocument.Paragraphs
If InStr(oPar.Range.Text, "XXX") > 0 Then
lngCount = lngCount + 1
DoEvents
'Do something with found text
End If
Next
MsgBox lngCount
lbl_Exit:
Exit Sub
End Sub
Sub Example3()
'Even faster
Dim oPar As Paragraph
Dim lngCount As Long
Set oPar = ActiveDocument.Paragraphs(1)
Do
If InStr(oPar.Range.Text, "XXX") > 0 Then
lngCount = lngCount + 1
'Do something with found text
End If
Set oPar = oPar.Next
Loop Until oPar.Range.End = ActiveDocument.Range.End
MsgBox lngCount
End Sub
Sub Example4()
'Likely faster yet because we only look a the paragraphs containing the text to find.
Dim oRng As Range
Dim oPar As Paragraph
Dim lngCount As Long
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "XXX"
While .Execute
lngCount = lngCount + 1
'Do something with found text
oRng.Collapse wdCollapseEnd
Wend
End With
MsgBox lngCount
End Sub
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Bold Button Runs Macro
|
DRD992 | Word VBA | 1 | 11-22-2017 02:23 PM |
Word slows down as file size increases
|
johnmill@dymeroaks.net | Word | 3 | 02-05-2016 05:49 PM |
Word periodically slows / freezes when typing or printing
|
KathyReid | Word | 3 | 09-26-2015 10:49 AM |
Help:word editing slows down when copying a block diagram
|
seeker_123 | Word | 1 | 03-02-2015 09:26 AM |
Word slows to a crawl.
|
traumatiziert | Word | 1 | 04-18-2012 12:42 AM |