word VBA 批量自动搜索并提取带有特定关键词的内容

180it 2019-09-25 PM 7067℃ 0条

在做数据筛选时,会要求提取带有特定关键词的短句。
楼主比较懒,代码只提供了提取关键词短句的部分,并未加入重复检测功能
待提取的word文档格式如下:(关键词为XX)
aaaxxaa
bbbxxbb
sssss
ccccxxcc
sddssfsdf
sdfsdfxxdddd

以下代码能够实现批量提取出word文档内的带有关键词的数据

Sub 提取内容()

Dim temp_text, text_output As String
i = 0
Do
With Selection.find
    .Text = "需要搜索的关键词"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
End With
Selection.find.Execute
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
i = i + 1
text_output = text_output & Selection.Text & Chr(13)
    If i = 500 Then
    Exit Do
    End If
Selection.EndKey Unit:=wdLine
Selection.MoveRight
Loop

Documents.Add.Content.Text = text_output
ActiveDocument.SaveAs ("路径\1.docx")  '输出成独立的word文档

End Sub

生成的1.docx的格式
aaaxxaa
bbbxxbb
ccccxxcc
sdfsdfxxdddd

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

word VBA 批量自动搜索并提取带有特定关键词的内容