在 "textbox" 中查找文本

Finding text in a "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