1

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
12
  • 1
    Set wordDoc = wordDoc = wordApp.Documents.Open("C:\Users\cd\LEQdoc.docx") should be Set wordDoc = wordApp.Documents.Open("C:\Users\cd\LEQdoc.docx") Commented Aug 23, 2019 at 17:55
  • 2
    ActiveDocument is not an Excel concept, so you need to preface that with wordApp i.e. wordApp.ActiveDocument Commented Aug 23, 2019 at 17:56
  • 1
    Also ensure that you declare the constants wdFindContinue, wdReplaceAll etc since you are late binding with MS Word Commented Aug 23, 2019 at 18:00
  • 1
    @SiddharthRout - snap! Commented Aug 23, 2019 at 18:01
  • 1
    Change Dim myStoryRange As Range to Dim myStoryRange As Object Commented Aug 23, 2019 at 18:07

1 Answer 1

4

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

In Action

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

This is great! It no longer gets hung up on that line and continues past it. However, it doesn't actually replace the text that it was supposed to find and replace.
I have added the screenshot. You may have to refresh th epage to see it
BTW I hope you declared Const wdReplaceAll = 2 on the top of your code ;)?
Works perfectly! Thanks for the help. Always much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.