View Single Post
 
Old 01-18-2023, 09:46 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,977
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

There are two obvious ways to speed up your code.
1. Avoid the selection object to reduce the need for screen scrolling
2. Search each phrase (URL) instead of each word since google.com.au is three separate words and it really should be done as a single find/replace
Code:
Sub NewWordReplacement2()
  Dim sCheckDoc As String
  Dim docRef As Document, docCurrent As Document
  Dim i As Integer, arrFind() As String, sFind As String

  sCheckDoc = "C:\Folder\Textfile.txt"
  Set docCurrent = ActiveDocument
  Set docRef = Documents.Open(sCheckDoc)
  arrFind = Split(docRef.Range.Text, vbCrLf)
  docRef.Close
  Options.DefaultHighlightColorIndex = wdRed
  With docCurrent.Range.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Font.Bold = True
    .Replacement.Highlight = True
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchWholeWord = False
    .MatchCase = True
    .MatchWildcards = False
    For i = LBound(arrFind) To UBound(arrFind)
      sFind = Trim(arrFind(i))
      If Len(sFind) > 1 Then
        .Text = sFind
        .Execute Replace:=wdReplaceAll
      End If
    Next i
  End With
End Sub
If this code improvement is still too slow, you could also pause the screen refresh while it runs.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote