![]() |
|
#1
|
||||
|
||||
![]()
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 |
#2
|
|||
|
|||
![]()
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 |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
AtaLoss | Word VBA | 37 | 09-22-2021 12:04 PM |
![]() |
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 |