您的位置:首页 > 编程语言 > VB

VBA word自动排版(8)——批量自动搜索并提取带有特定关键词的内容

2019-02-19 15:50 363 查看

在做数据筛选时,会要求提取带有特定关键词的短句。
楼主比较懒,代码只提供了提取关键词短句的部分,并未加入重复检测功能
待提取的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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: