Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 01-18-2023, 09:46 PM
Guessed's Avatar
Guessed Guessed is offline Highlight word document with words from .txt file Windows 10 Highlight word document with words from .txt file Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,185
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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Highlight word document with words from .txt file Find and highlight multiple words in MS Word document AtaLoss Word VBA 37 09-22-2021 12:04 PM
Highlight word document with words from .txt file Macro To Identify & Highlight Words In MS Word Based Upon A List In Excel File Column abhimanyu Word VBA 5 03-20-2020 01:33 PM
How to find (highlight) two and more words in a list of 75k single words in Word 2010 Usora Word 8 05-29-2018 03:34 AM
Find and highlight multiple words in MS Word document qkjack Word VBA 7 02-21-2018 07:09 PM
Macro to highlight repeated words in word file and extract into excel file aabri Word VBA 1 06-14-2015 07:20 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:52 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