0

I have an Excel program.

Which is manipulating a word document and find/replace text. My code looks like this

    Dim wApp As Word.Application
Dim wDoc As Word.Document

Set wApp = CreateObject("Word.Application")
wApp.Visible = False
Set wDoc = wApp.Documents.Open(myFile)
                        
With wDoc.Content.Find
    .Text = "<Customer Name>"
    .Replacement.Text = "Customer Name"
    .Wrap = wdFindContinue
    .MatchWholeWord = True
    .Execute Replace:=wdReplaceAll
                
    .Text = "<Billing Address>"
    .Replacement.Text = "Billing Address"
    .Wrap = wdFindContinue
    .MatchWholeWord = True
    .Execute Replace:=wdReplaceAll
End With

Which works perfectly.

However the word document has some words within a text box which I want to find and replace on, but I can't get it to work (the above code only searches the main body of the word document and not the text boxes)

I tried

    wDoc.Shapes.Range(Array("Text Box 14")).Select
With wDoc.Selection.Find
    .Text = "<Profile Code>"
    .Replacement.Text = "Profile Code"
    .Wrap = wdFindContinue
    .MatchWholeWord = True
    .Execute Replace:=wdReplaceAll
End With

but that didn't work. I got an Object doesn't support this property or method error message

Please help!

1 Answer 1

1

Provided the text box is named "Text Box 14" the following should work for you (it works for me in O365)

   With wDoc.Shapes("Text Box 14").TextFrame.TextRange.Find
      .Text = "<Profile Code>"
      .Replacement.Text = "Profile Code"
      .Wrap = wdFindContinue
      .MatchWholeWord = True
      .Execute Replace:=wdReplaceAll
   End With
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.