识别在一个给定的页面中没有出现任何字符。

问题描述 投票:0回答:2

我面临着Word文档中断断续续散落的空白页,我想把它们丢弃。 但我需要先用VBA识别它们。 如果我点击工作表的话,每个工作表中都会有一些点,在这些点上会有一个卡擦落地。

但我不知道如何从那里进展。 如何通过VBA指导Word识别工作表中是否有内容?

vba ms-word word-vba
2个回答
2
投票

这几条线的东西可能适合。

ActiveDocument.Repaginate
j = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)

For i = j To 1 Step -1
    NotEmpty = True
    Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=CStr(i)
    Selection.GoTo What:=wdGoToBookmark, Name:="\page"
    If Selection.Characters.Count < 3 Then
        NotEmpty = False
        For Each c In Selection.Characters
            If Asc(c) > 13 Then
                ''Possibly not empty
                NotEmpty = True
            End If
        Next
    End If
    If NotEmpty = False Then
        Selection.Delete
    End If
Next

0
投票

现在在2020年。从上面的版本稍作修改。在我的情况下,我需要检查 sel.Characters.Count <= 3. 谢谢!

Public Function DeleteBlankPages()
Dim i, j, notEmpty, c
Dim sel As Selection

myWordDoc.Activate

ActiveDocument.Repaginate

Set sel = ActiveDocument.ActiveWindow.Selection

j = ActiveDocument.BuiltinDocumentProperties(wdPropertyPages)

For i = j To 1 Step -1
    notEmpty = True
    sel.GoTo What:=wdGoToPage, Which:=wdGoToNext, Name:=CStr(i)
    sel.GoTo What:=wdGoToBookmark, Name:="\page"

    'Debug.Print "Page", i, " count", sel.Characters.Count

    If sel.Characters.Count <= 3 Then
        notEmpty = False
        For Each c In sel.Characters
            If Asc(c) > 13 Then
                ''Possibly not empty
                notEmpty = True
            End If
        Next
    End If

    If notEmpty = False Then
        sel.Delete
    End If
Next

结束功能

© www.soinside.com 2019 - 2024. All rights reserved.