Excel VBA Replace Words in Word doc with changing cell values

Hi

What Excel VBA code would I use to replace any word with any other word.
I am looping through Excel cells that have different names in them every day and need to replace
certain words in a Word doc with these
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saqib Husain, SyedEngineerCommented:
Can you upload a sample to make the requirement very clear?
0
GrahamSkanRetiredCommented:
This expects the existing words to be in column 1 and the new words to be in column 2
Sub ReplaceWordText()
    Dim doc As Word.Document
    Dim objWord As Word.Application
    Dim strFile As String
    Dim r As Integer
    Dim sh As Worksheet
    Dim bNewInstance As Boolean
    
    strFile = "C:\MYDocuments\MyFile.doc"
    'avoid creating multiple instances of the Word application
    On Error Resume Next 'suppress error checking for the next instruction
    Set objWord = GetObject(, "Word.Application")
    On Error GoTo 0 'resume error checking
    
    If objWord Is Nothing Then
        'no instance, so make a new one
        Set objWord = CreateObject("Word.Application")
        bNewInstance = True
    End If
    objWord.Visible = True
    
    Set doc = objWord.Documents.Open(strFile)
    
    Set sh = ActiveSheet
    r = 2
    Do Until sh.Cells(r, 1) = ""
        With doc.Find
            .Text = sh.Cells(r, 1)
            .Replacement.Text = sh.Cells(r, 2)
            .Execute Replace:=wdReplaceAll
        End With
        r = r + 1
    Loop
    doc.Close wdSaveChanges
     If bNewInstance Then
         objWord.Quit
    End If
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Great answer. Thanks very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.