Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-20-2020, 02:06 PM
gmaxey gmaxey is offline MS WORD search macro slows way down as it runs Windows 10 MS WORD search macro slows way down as it runs Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,636
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default


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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
MS WORD search macro slows way down as it runs Bold Button Runs Macro DRD992 Word VBA 1 11-22-2017 02:23 PM
MS WORD search macro slows way down as it runs Word slows down as file size increases johnmill@dymeroaks.net Word 3 02-05-2016 05:49 PM
MS WORD search macro slows way down as it runs Word periodically slows / freezes when typing or printing KathyReid Word 3 09-26-2015 10:49 AM
MS WORD search macro slows way down as it runs Help:word editing slows down when copying a block diagram seeker_123 Word 1 03-02-2015 09:26 AM
MS WORD search macro slows way down as it runs Word slows to a crawl. traumatiziert Word 1 04-18-2012 12:42 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:39 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft