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

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

你可能感兴趣的:(VBA编程,数据库,数据提取,数据优化)