批量删除多个WORD文档最后一页

发布于 2020-10-22  479 次阅读


用 VBA 代码来解决:

首先需要打开 Word 中的 Visual Basic 编辑器,并插入一个「模块」:

1.jpg

在 Word 中打开 Visual Basic 编辑器,并插入「模块」

在新插入的模块的编辑器中输入以下代码,然后点击顶部菜单栏中的三角按钮来运行:

2.jpg

在 Viisual Basic 编辑器中运行 VBA 代码

在弹出的选择窗口中选择要处理的一个或多个 Word 文档(.doc 或 .docx),并点击 “打开”。

3.jpg

等待程序运行,完成后会弹框提示:

4.png

上面用到的 VBA 代码 [1][2][3][4] 如下:

注意:运行下面代码时需要谨慎,因为一旦运行结束便无法撤销。对于重要的文件资料,建议运行之前先进行备份,以免程序中途报错而丢失最后一页的内容。
在正式运行此程序之前,也建议大家先创建几个测试用的 Word 文档,在它们上面运行此程序,确认没有问题后再在自己的文档上来运行,以免造成损失。

Sub DeleteLastPageOfDocs()

Dim fd As FileDialog
Dim aDoc As Document
Dim i As Long
Dim count As Long

Set fd = Application.FileDialog(FileDialogType:=msoFileDialogOpen)
With fd
    .AllowMultiSelect = True
    .Title = "请选择要处理的一个或多个 Word 文档"
    .Filters.Add "Word 文档", "\*.doc; \*.docx", 1
    If .Show = -1 Then
        count = .SelectedItems.count
        For Each vrtSelectedItem In .SelectedItems
            Set aDoc = Documents.Open(vrtSelectedItem)
            With ActiveDocument
                .Bookmarks("\\EndOfDoc").Range.Select
                .Bookmarks("\\Page").Range.Delete
                'Delete the last manual page break if there isn't any text after it
                For i = ActiveDocument.Paragraphs.count To 1 Step -1
                  If Asc(.Paragraphs(i).Range.Text) = 12 Then
                    .Paragraphs(i).Range.Delete
                    Exit For
                  End If
                  If Len(.Paragraphs(i).Range.Text) > 1 Then
                    Exit For
                  End If
                Next i
            End With
            aDoc.Save
            aDoc.Close
        Next
        MsgBox "已处理 " & count & " 个 Word 文档"
    End If
End With

End Sub

【更新】

修复了最后两页的页面方向不同时导致的问题,下面的代码在删除最后一页后,不再会将原来的倒数第二页的页面方向修改成与最后一页相同了:

注意,由于在代码中增加了修改最后一页页面布局的操作 [5][6],代码的整体运算速度会比前面一版的代码速度更慢。如果你处理的文档没有页面方向不同的问题,建议还是使用上面的代码,速度更快一些。

Sub DeleteLastPageOfDocs()

Dim fd As FileDialog
Dim aDoc As Document
Dim i As Long
Dim count As Long
Dim orient As Long
Dim flag As Boolean

Set fd = Application.FileDialog(FileDialogType:=msoFileDialogOpen)
With fd
    .AllowMultiSelect = True
    .Title = "请选择要处理的一个或多个 Word 文档"
    .Filters.Add "Word 文档", "\*.doc; \*.docx", 1
    If .Show = -1 Then
        count = .SelectedItems.count
        For Each vrtSelectedItem In .SelectedItems
            flag = False
            Set aDoc = Documents.Open(vrtSelectedItem)
            With ActiveDocument
                'Move cursor to the end of document
                Selection.EndKey Unit:=wdStory
                'Check whether the last and the last but one pages have the same page orientation
                nPageC = .ActiveWindow.ActivePane.Pages.count
                .GoTo(wdGoToPage, wdGoToAbsolute, nPageC - 1).Select
                orient = Selection.PageSetup.Orientation
                .GoTo(wdGoToPage, wdGoToAbsolute, nPageC).Select
                Debug.Print orient & " <-- " & Selection.PageSetup.Orientation
                If Selection.PageSetup.Orientation <> orient Then flag = True
                'Delete content in the last page
                .Bookmarks("\\EndOfDoc").Range.Select
                .Bookmarks("\\Page").Range.Delete
                'Change page orientation
                If flag Then Selection.PageSetup.Orientation = orient
                'Delete the last manual page break if there isn't any text after it
                For i = ActiveDocument.Paragraphs.count To 1 Step -1
                  If Asc(.Paragraphs(i).Range.Text) = 12 Then
                    .Paragraphs(i).Range.Delete
                    Exit For
                  End If
                  If Len(.Paragraphs(i).Range.Text) > 1 Then
                    Exit For
                  End If
                Next i
            End With
            aDoc.Save
            aDoc.Close
        Next
        MsgBox "已处理 " & count & " 个 Word 文档"
    End If
End With

End Sub

参考

  1. ^Remove multiple pages from multiple documents https://answers.microsoft.com/en-us/msoffice/forum/all/remove-multiple-pages-from-multiple-documents/23d6e62d-d240-467b-923f-7ee63ba7851e
  2. ^loop through all selected items in dialog http://www.vbaexpress.com/forum/showthread.php?16036-loop-through-all-selected-items-in-dialog
  3. ^Word VBA -- Delete last page https://www.experts-exchange.com/questions/29067732/Word-VBA-Delete-last-page.html
  4. ^How to delete the last manual page break if there isn't any text after it (with VBA)? https://answers.microsoft.com/en-us/msoffice/forum/all/how-to-delete-the-last-manual-page-break-if-there/92cb9fa0-187e-446f-bf7e-df1234d1da54
  5. ^[求助] [已解决] 如何使用 WordVBA 遍历所有页面? http://club.excelhome.net/thread-1118598-1-1.html
  6. ^PageSetup.Orientation property (Word) https://docs.microsoft.com/en-us/office/vba/api/word.pagesetup.orientation

或许明日太阳西下倦鸟已归时