【128】Word中的VBA

通过查找关键字,然后删除整段文字的实现!

Sub 删除查找含关键词的行()
    Dim KeyWord As String
    KeyWord = InputBox("请输入关键词(词长不限,中英均可):", "关键词设置", "")
    If KeyWord = "" Then Exit Sub
    Selection.HomeKey wdStory
    Do
        With Selection.Find
            .Text = KeyWord
            .Wrap = wdFindStop
            .Forward = True
        End With
        If Selection.Find.Execute = False Then Exit Do
            Selection.Paragraphs(1).Range.Select
            If (Left(Selection.Text, Len(KeyWord))) = KeyWord Then
            Selection.Paragraphs(1).Range.delete
            Selection.HomeKey unit:=wdLine
        End If
    Loop
    Selection.HomeKey unit:=wdStory
End Sub

参考:百度知道

删除空白行

Sub DelBlank()
    Dim i as Paragraph, n as Long
    Application.ScreenUpdating = False
    For Each i In ActiveDocument.Paragraphs
        If Len(i.Range) = 1 Then
            i.Range.Delete
            n = n + 1
        End If
    Next
    MsgBox "共删除空白段落" & n & "个。"
    Application.ScreenUpdating = True
End Sub

参考:百度知道

原文地址:https://www.cnblogs.com/alex-bn-lee/p/3365486.html