使用.Find查找文本

法官

我正在编写一个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, iShp As Word.InlineShape
For Each shp In ActiveDocument.Shapes
    If shp.Type = msoTextBox Then
        If shp.TextFrame.HasText Then
            'do something
        End If
    End If
Next

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章