Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-18-2023, 10:45 PM
gmayor's Avatar
gmayor gmayor is offline Highlight word document with words from .txt file Windows 10 Highlight word document with words from .txt file Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

There's a third way - and that's to read the text file directly to an array without opening it e.g. Using Andrew's code as an example.



Code:
Sub ReplacefromTXT()
'Graham Mayor - https://www.gmayor.com - Last updated - 19 Jan 2023
Dim FSO As Object, oFile As Object
Dim arrFind() As String, sFind As String
Dim oDoc As Document
Dim i As Integer
Const sName As String = "C:\Test\Test.txt" ' change this to your text file full name

       
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set oFile = FSO.OpenTextFile(sName, 1)
    arrFind = Split(oFile.ReadAll, vbNewLine)

    Set oDoc = ActiveDocument
    Options.DefaultHighlightColorIndex = wdRed
    With oDoc.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
lbl_Exit:
    Set FSO = Nothing
    Set oFile = Nothing
    Set oDoc = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #2  
Old 01-19-2023, 05:08 PM
Big_Sugah Big_Sugah is offline Highlight word document with words from .txt file Windows 10 Highlight word document with words from .txt file Office 2016
Novice
Highlight word document with words from .txt file
 
Join Date: Jan 2023
Posts: 5
Big_Sugah is on a distinguished road
Default

I have tried both of those but I am now getting the error
vbaerror.png

I am assuming I need to limit the size of whatever text .Text finds
Reply With Quote
Reply



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 01:01 PM.


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