How can I find and replace text in Word using Excel VBA

0 votes

I am using Excel VBA to open a document in Word. Once the document is open the goal is to search for "InsuranceCompanyName" and replace it with the company's name.

I have tried

wordDoc.Find.Execute FindText:="InsuranceCompanyName", ReplaceWith:="Fake Ins Co"

and

wordDoc.Replace What:="InsuranceCompanyName", Replacement:="Fake Ins Co"

and also

For Each myStoryRange In ActiveDocument.StoryRanges

    With myStoryRange.Find
        .Text = "InsuranceCompanyName"
        .Replacement.Text = "Fake Ins Co"
        .WrapText = wdFindContinue
        .Execute Replace:=wdReplaceAll
    End With 
Next myStoryRange

The full code is listed below.

Sub FindReplace()

Dim wordApp As Object 
Dim wordDoc As Object 
Dim myStoryRange As Range

'sets up the word app
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True 

'opens the document that we need to search through 
Set wordDoc = wordDoc = wordApp.Documents.Open("C:\Users\cd\LEQdoc.docx")

'here is where the find and replace code would go

End Sub 

For the first method I get the error:

Object doesn't support this property or method.

For the second: the same error

The third method:

argument not optional

In regards to the .Find in

With myStoryRange.Find
Oct 15 in Others by Kithuzzz
• 20,660 points
115 views

1 answer to this question.

0 votes

Try this code

Option Explicit

Const wdReplaceAll = 2

Sub FindReplace()
    Dim wordApp As Object
    Dim wordDoc As Object
    Dim myStoryRange As Object

    '~~> Sets up the word app
    Set wordApp = CreateObject("Word.Application")
    wordApp.Visible = True

    '~~> Opens the document that we need to search through
    Set wordDoc = wordApp.Documents.Open("C:\Users\routs\Desktop\Sample.docx")

    For Each myStoryRange In wordDoc.StoryRanges
        With myStoryRange.Find
            .Text = "InsuranceCompanyName"
            .Replacement.Text = "Fake Ins Co"
            .Execute Replace:=wdReplaceAll
        End With
    Next myStoryRange
End Sub
answered Oct 15 by narikkadan
• 37,660 points

Related Questions In Others

0 votes
1 answer

How should I show the Developer tab in Excel using Macros? (Excel VBA)

You can activate (mode=1) or deactivate (mode=0) ...READ MORE

answered Nov 15 in Others by narikkadan
• 37,660 points
37 views
0 votes
1 answer

How do I insert a WebP-image (".jpg") in Excel using VBA?

It's not currently on the list of ...READ MORE

answered Nov 15 in Others by narikkadan
• 37,660 points
45 views
0 votes
1 answer

How do I get an Excel range using row and column numbers in VSTO / C#?

Use: int countRows = xlWorkSheetData.UsedRange.Rows.Count; int countColumns = xlWorkSheetData.UsedRange.Columns.Count; object[,] ...READ MORE

answered Nov 17 in Others by narikkadan
• 37,660 points
41 views
0 votes
1 answer

How do delete an specific page in a word document using Excel VBA?

Check the code below. I eliminated the ...READ MORE

answered Nov 21 in Others by narikkadan
• 37,660 points
29 views
0 votes
1 answer

Trying to create an enclosing bookmark after pasting a picture from Excel into Word using VBA

Try this: Sub IMPORT_TO_WORD() Dim ws1 As Worksheet, ws2 ...READ MORE

answered Oct 30 in Others by narikkadan
• 37,660 points
44 views
0 votes
1 answer

Indent table pasted from Excel into Word

It is feasible to pick up the ...READ MORE

answered Oct 30 in Others by narikkadan
• 37,660 points
46 views
0 votes
1 answer

Word Mail Merge with Excel data has to be saved in different files with custom names

Try this: Public Sub Mail_Merge() On Error GoTo ErrH Dim ...READ MORE

answered Nov 6 in Others by narikkadan
• 37,660 points
53 views
0 votes
1 answer

Copy Text from Range in Excel into Word Document

Here are some articles that may help: Control ...READ MORE

answered Nov 6 in Others by narikkadan
• 37,660 points
36 views
0 votes
1 answer

Multiple find and replace in MS Word from a list in MS Excel

If I understand you correctly, you want ...READ MORE

answered Oct 28 in Others by narikkadan
• 37,660 points
46 views
0 votes
1 answer

Excel - How can I get the average of cells where the value in one column is X and the value in another column is Y?

Use AVERAGEIFS ... =AVERAGEIFS(C2:C13,A2:A13,"Yellow Typ ...READ MORE

answered Nov 11 in Others by narikkadan
• 37,660 points
43 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP