在Word中查找文本,然后从范围中选择内容

时间:2015-10-14 14:20:48

标签: excel excel-vba ms-word vba

我想在Word文档中找到一些单词或句子。然后我想在该行下面选择包含单词/句子(包括该行)的整个文档。之后,复制内容并将其粘贴到Excel工作表中。

我坚持选择范围。

编辑:只有一个独特的句子。

请查看我的代码:

Sub importDocfile()
    Dim rng0 As Word.Range
    Dim WordApp As Object, WordDoc As Object
    Dim myRange As Word.Range
    Dim strTheText As String
    Set WordApp = CreateObject("Word.Application")
    'word ll be close to run
    WordApp.Visible = True
    'open the file .doc
    Set WordDoc = WordApp.Documents.Open(ThisWorkbook.Path & "\Personality    analysis and profile.docx")
    Set myRange = WordApp.ActiveDocument.Content
    myRange.Find.Execute FindText:="OPQ32" ' Here I dont know how to declare my start range
End Sub

1 个答案:

答案 0 :(得分:0)

这对你有用吗?

Set myRange = ActiveDocument.Range
myRange.Find.Execute "OPQ32"

Set myRange = ActiveDocument.Range( _
    Start:=myRange.Start, _
    End:=ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.End)