0

I have this line of code which does a search and replace in a word document:

oDoc.Content.Find.Execute(FindText:="Invoice", ReplaceWith:="Invoice - Paid: " + paid_date, Replace:=word.WdReplace.wdReplaceAll)

Is there any way, i can do the search and replace inside a specific textbox in word rather than the whole document?

1 Answer 1

1

Here is one way to go about it.

    For Each oCtl As Shape In doc.Shapes
        If oCtl.Type = Microsoft.Office.Core.MsoShapeType.msoTextBox Then
            oCtl.TextFrame.TextRange.Text.Replace("Invoice", "Invoice - Paid: " + paid_date)
        End If
    Next

This essentially searches all the text boxes in your document and replaces "Invoice"

As an attempt to find the name of the textbox try this...

    For Each oCtl As Shape In doc.Shapes
        If oCtl.Type = Microsoft.Office.Core.MsoShapeType.msoTextBox Then
            MsgBox("Name: " & oCtl.Name & vbNewLine & "ID: " & oCtl.ID & vbNewLine & "Title: " & oCtl.Title & vbNewLine & "Text: " & oCtl.TextFrame.TextRange.Text)
        End If
    Next

This should give you unique or identifiable items of every textbox.

then you could just go like this -

If oCtl.Name = "1234" then oCtl.Text.replace(whatever)

or ID or Title or whatever you decide to choose.

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

4 Comments

is there a way to search a specific textbox just in case there is another instance of the same word. i just want to replace one text box
Does the text box have a name or ID Number? is it on a specific page? ... In order to change a specific text box you need to tell me what's specific or special about it to correctly identify it from other textboxes -- even if its just a special word inside the box
There is a word (UNPAID) inside the box but I don't want to find and replace a specific word just in case there is 2 instances of it on the page. Is there a way to give the text box a name or ID and then find and replace just inside that text box?
is there a way of preserving formatting with this approach?

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.