在"textbox"中查找文本



我正在编写一个Excel VBA代码段,它在docx中查找文本字符串。它适用于常规文本,但如果字符串位于";文本框";在docx中(尽管在MS Word中手动搜索会验证它是否存在(。

Dim tempDoc As Word.Document
Set tempDoc = Documents.Open(fileName:=foundName, Format:=wdOpenFormatAuto, Visible:=False)
Dim fromDate As Word.Range
Set fromDate = tempDoc.Range 
Dim fromCopyRange As Word.Range

fromDate.Find.Execute FindText:="covers period", MatchCase:=False
If fromDate.Find.Found = True Then 'this normally works, except if the text is in a textbox

Set fromCopyRange = tempDoc.Range(Start:=fromDate.End + 1, End:=fromDate.End + 20)
foundResult = Trim(fromCopyRange.Text)
Debug.Print foundResult
End If

您需要在代码中添加这样的内容。

Dim shp As Word.Shape
For Each shp In ActiveDocument.Shapes
If shp.Type = msoTextBox Then
If shp.TextFrame.HasText Then
'do something
End If
End If
Next

最新更新