如何用vba批量删除Word文档中的空白页?

word文档中的空白页,可以是没有任何内容只含有段落分隔符的页面,也可以是含有任意多个无效字符的页面。

为了批量删除Word文档中的空白页,可以使用如下的代码:

Sub QQ1722187970()
    Const wdPageBreak = 7
    Const wdSectionBreakContinuous = 3
    Const wdSectionBreakNextPage = 2
    Const wdGoToPage = 1
    Const wdGoToAbsolute = 1
    Dim oDoc As Document
    Dim oRng As Range
    Set oDoc = Word.ActiveDocument
    Dim iPageNo As Long
    '获取总页数
    With oDoc
    iPageNo = .Range.Information(wdNumberOfPagesInDocument)
        For i = iPageNo To 1 Step -1
            '定位到页开始
            Set oRng = .GoTo(wdGoToPage, Which:=wdGoToAbsolute, Count:=i)
            oRng.Select
            oRng.SetRange oRng.Start, oRng.Bookmarks("\page").End
            sText = oRng.Text
            '判断当前页面是否含有有效的字符
            If Not (sText Like "*[0-9]*" Or sText Like "*[a-z]*" Or sText Like "*[A-Z]*" Or sText Like "*[一-龥]*") Then
                oRng.Delete
            End If
        Next i
    End With
End Sub
       

仅有1条评论 发表评论

  1. Pingback引用通告: 如何用vba在word中快速移动或选中内容区域? – 算法网 /

发表评论