[英]vb.net find and replace text inside a textbox in Word
我有這行代碼可以在word文檔中進行搜索和替換:
oDoc.Content.Find.Execute(FindText:="Invoice", ReplaceWith:="Invoice - Paid: " + paid_date, Replace:=word.WdReplace.wdReplaceAll)
有什么辦法,我可以在Word中而不是整個文檔中在特定文本框中進行搜索和替換?
這是解決問題的一種方法。
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
這實際上會搜索文檔中的所有文本框,並替換為“發票”
為了找到文本框的名稱,請嘗試以下操作...
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
這應該為您提供每個文本框的唯一或可識別的項目。
那你就可以這樣-
If oCtl.Name = "1234" then oCtl.Text.replace(whatever)
或ID或標題或您決定選擇的任何內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.