1

I have an embedded word document in my worksheet, names "Rec1"

So using before:

Sub ReplaceTextinOLEObject
    Dim oDoc As OLEObject
    Set oDoc = Worksheets("Sheet1").OLEObjects("Rec1")
    oDoc.Activate
    With oDoc.Content.Find
        .ClearFormatting
        .Text = "hi"
        .Replacement.ClearFormatting
        .Replacement.Text = "hello"
        .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
    End With
    Word.Application.Quit wdDoNotSaveChanges
End Sub

When perform running above, the With part committed below error:

Run-time error '438':
object doesn't support this property or method

What is the problem of the using Find object for OLE embedded word documents?

1 Answer 1

1

The issue here is that the OLEObjects has no .Content property. That is also what the error tells you.

Therefore you have to use oDoc.Object.Content.Find instead. And you don't need to oDoc.Activate the document, it will also work without.

The following should work:

Public Sub ReplaceTextinOLEObject
    Dim oDoc As OLEObject
    Set oDoc = Worksheets("Sheet1").OLEObjects("Rec1")
    'oDoc.Activate 'remove this line, activate is not needed here
    With oDoc.Object.Content.Find
        .ClearFormatting
        .Text = "hi"
        .Replacement.ClearFormatting
        .Replacement.Text = "hello"
        .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
    End With
    Word.Application.Quit wdDoNotSaveChanges
End Sub

Note: Of course the "Microsoft Word 16.0 Object Library" (version may vary) needs to be referenced to run this code.

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

Comments

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.