charlie
charlie

Reputation: 481

vb.net find and replace text inside a textbox in Word

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?

Upvotes: 0

Views: 2471

Answers (1)

Nefariis
Nefariis

Reputation: 3549

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.

Upvotes: 1

Related Questions