View Single Post
 
Old 08-09-2019, 08:30 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,137
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

This is fairly straightforward. You cannot read from Notepad, however you can read from a text file created by Notepad. Assuming the space between the columns in the list is a tab character (as copied from Excel) then the following will find the first part in the body of the document and replace it with the second. If there are other areas to process, you will have to loop through the story ranges to use this code.

Code:
Sub ReplaceFromTextFile()
Dim oRng As Range
Dim sFindText As String, sReplacement As String
Dim iNum As Integer
Dim sData As String
Dim vData As Variant
Const sName As String = "C:\Path\Forums\list.txt"

    iNum = FreeFile()
    Open sName For Input As #iNum

    While Not EOF(iNum)
        Line Input #iNum, sData
        vData = Split(sData, Chr(9))
        Set oRng = ActiveDocument.Range
        sFindText = vData(0)
        sReplacement = vData(1)
        With oRng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            Do While .Execute(FindText:=sFindText, _
                              MatchWholeWord:=True, _
                              MatchWildcards:=False, _
                              Forward:=True, _
                              Wrap:=wdFindStop) = True
                oRng.Text = sReplacement
                oRng.Collapse wdCollapseEnd
            Loop
        End With
    Wend
lbl_Exit:
    Set oRng = 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