跳到主要內容

如何每5或n頁將Word文檔拆分為多個單獨的文件?

如果您現在有一個包含數百頁的大型Word文檔,則希望每10或n頁將此文檔拆分為多個單獨的文件。 有沒有一種快速簡便的方法可以解決這項工作,而無需一頁一頁地複制和粘貼頁面?

使用VBA代碼每10或n頁將Word文檔拆分為單獨的文件

每隔10或n頁將Word文檔拆分為單獨的文件,具有驚人的功能


使用VBA代碼每10或n頁將Word文檔拆分為單獨的文件

要將每10或n頁的大型文檔拆分為多個單獨的文件,請使用以下VBA代碼,具體操作如下:

1。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 窗口。

2。 然後,單擊 插入 > 模塊,將以下代碼複製並粘貼到打開的空白模塊中:

VBA代碼:每10或n頁將文檔拆分為多個單獨的文件:

Sub DocumentSplitter()
    Dim xDoc As Document, xNewDoc As Document
    Dim xSplit As String, xCount As Long, xLast As Long
    Dim xRngSplit As Range, xDocName As String, xFileExt As String
    Dim xRegEx As RegExp
    Dim xPageCount As Integer
    Dim xShell As Object, xFolder As Object, xFolderItem As Object
    Dim xFilePath As String
    On Error Resume Next
    Set xDoc = Application.ActiveDocument
    Set xShell = CreateObject("Shell.Application")
    Set xFolder = xShell.BrowseforFolder(0, "Select a Folder:", 0, 0)
    If TypeName(xFolder) = "Nothing" Then Exit Sub
    Set xFolderItem = xFolder.Self
    xFilePath = xFolderItem.Path & "\"
    Application.ScreenUpdating = False
    Set xNewDoc = Documents.Add(Visible:=False)
    xDoc.Content.WholeStory
    xDoc.Content.Copy
    xNewDoc.Content.PasteAndFormat wdFormatOriginalFormatting
    With xNewDoc
        xPageCount = .ActiveWindow.Panes(1).Pages.Count
L1:     xSplit = InputBox("The document contains " & xPageCount & " pages." & _
                 vbCrLf & vbCrLf & " Please enter the page count you want to split:", "Kutools for Word", xSplit)
        If Len(Trim(xSplit)) = 0 Then Exit Sub
        Set xRegEx = New RegExp
        With xRegEx
            .MultiLine = False
            .Global = True
            .IgnoreCase = True
            .Pattern = "[^0-9]"
        End With
        If xRegEx.Test(xSplit) = True Then
            MsgBox "Please enter the page number:", vbInformation, "Kutools for Word"
            Exit Sub
        End If
        If VBA.Int(xSplit) >= xPageCount Then
            MsgBox "The number is greater than the document number." & vbCrLf & "Please re-enter", vbInformation, "Kutools for Word"
            GoTo L1
        End If
        xDocName = xDoc. Name
        xFileExt = VBA.Right(xDocName, Len(xDocName) - InStrRev(xDocName, ".") + 1)
        xDocName = Left(xDocName, InStrRev(xDocName, ".") - 1) & "_"
        xFilePath = xFilePath & xDocName
        For xCount = 0 To Int(xPageCount / xSplit)
            xPageCount = .ActiveWindow.Panes(1).Pages.Count
            If xPageCount > xSplit Then
                xLast = xSplit
            Else
                xLast = xPageCount
            End If
            Set xRngSplit = .GoTo(What:=wdGoToPage, Name:=xLast)
            Set xRngSplit = xRngSplit.GoTo(What:=wdGoToBookmark, Name:="\page")
            xRngSplit.Start = .Range.Start
            xRngSplit.Cut
            Documents.Add
            Selection.Paste
            ActiveDocument.SaveAs FileName:=xFilePath & xCount + 1 & xFileExt, AddToRecentFiles:=False
            ActiveWindow.Close
        Next xCount
        Set xRngSplit = Nothing
        xNewDoc.Close wdDoNotSaveChanges
        Set xNewDoc = Nothing
    End With
    Application.ScreenUpdating = True
End Sub

3。 粘貼上面的代碼後,仍然在 Microsoft Visual Basic for Applications 窗口,請點擊 工具 > 參考,然後彈出 參考項目 對話框,檢查 Microsoft VBScript正則表達式5.5 在選項 可用參考 列錶框,請參見屏幕截圖:

4。 然後點擊 OK 按鈕,然後按 F5 運行此代碼的關鍵,以及 瀏覽文件夾 顯示對話框,請選擇要在其中放置拆分文件的文件夾,請參見屏幕截圖:

5。 然後點擊 OK 按鈕,並彈出另一個提示框,提醒您輸入要分割的頁數,請參見屏幕截圖:

6。 然後點擊 OK 按鈕,活動的Word文檔每10頁被拆分成幾個單獨的文件,您可以轉到指定的文件夾以查看結果:


每隔10或n頁將Word文檔拆分為單獨的文件,具有驚人的功能

Kutools for Word 具有強大的功能- 分裂 使用此實用程序的功能,您可以根據Heading1,分頁符,分節符和頁面將大型Word文檔快速拆分為多個單獨的文件。

提示:要應用此 分裂 功能,首先,您應該下載 Kutools for Word,然後快速輕鬆地應用該功能。

安裝後 Kutools for Word,請這樣做:

1。 點擊 Kutools 加 > 分裂,請參見屏幕截圖:

2。 在彈出的對話框中,請根據需要設置以下操作,請參見屏幕截圖:

3。 完成設置後,請點擊 OK,整個文檔將根據每個頁面分為多個文件,請參見屏幕截圖:

點擊下載Kutools for Word並立即免費試用!

最佳辦公生產力工具

Kutools for Word - 透過 Over 提升您的文字體驗 100 顯著特點!

🤖 Kutools 人工智慧助手:用人工智慧改變你的寫作 - 生成內容  /  重寫文字  /  總結文件  /  查詢資料 基於文檔,全部在Word中

📘 文件掌握: 分頁  /  合併文件  /  以各種格式匯出選擇(PDF/TXT/DOC/HTML...)  /  批次轉換為PDF  /  將頁面匯出為圖像  /  一次列印多個文件...

內容編輯: 批量查找和替換 跨多個文件  /  調整所有圖片的大小  /  轉置表行和列  /  將表格轉換為文字...

🧹 輕鬆清潔: 移開 多餘的空間  /  分節符  /  所有標題  /  文本框  /  超鏈接  / 如需更多拆卸工具,請前往我們的 刪除組...

創意插入: 插入 千位分隔符  /  複選框  /  單選按鈕  /  QR Code  /  條碼  /  對角線表  /  公式標題  /  圖片說明  /  表格標題  /  多張圖片  / 發現更多 插入組...

🔍 精準選擇:精確定位 特定頁面  /    /  形狀  /  標題段落  / 增強導航功能 更多 選擇功能...

星級增強: 快速導航至任何位置  /  自動插入重複文字  /  在文檔視窗之間無縫切換  /  11 轉換工具...

???? 想嘗試這些功能嗎? Kutools for Word 提供了 60天免費試用,沒有任何限制! 🚀
 
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
good things. i used it to split with 1 page 1 word document, it was successful except that each document (after split), it has 2 pages in total although 2nd page is always blank. 
This comment was minimized by the moderator on the site
this code gives compile error which shows user define type is not define
This comment was minimized by the moderator on the site
For me, the VBA creates a single document that is a copy of the original and that's it.
This comment was minimized by the moderator on the site
The VBscript code made Word crash. I was trying to split a 32Mb Word file with many pages but it seems Word can't handle it through VBscript.
Thanks anyway
This comment was minimized by the moderator on the site
Hello, Studia,
The VBA code may crash when there is a large document, it is not stable, so I recommend you use our Kutools for Word tool, it has updated, and support to solve this task, you can download it and free trial 30 day.
Please try, thank you!
This comment was minimized by the moderator on the site
VBA Script: Split a document into separate files every 10 or n pages not worked
This comment was minimized by the moderator on the site
Hello, Sergey,
The above code works well in my Word document, which Word version do you use?
And which step went wrong in your operation?
This comment was minimized by the moderator on the site
i have been trying to split a word file of 166 pages and it gives 166 files each with 166 pages?
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations