如何將 Word 文檔每 5 頁或 n 頁拆分為單獨的文件?
在處理大型 Word 文檔時,您可能需要將文檔拆分為較小的單獨文件,以便於處理、共享或編輯。每隔 5 頁、10 頁或指定數量的頁面進行拆分,可以是更有效地管理冗長內容的實用解決方案。
手動逐頁複製和粘貼既耗時又低效。在本教程中,我們將介紹快速且簡單的方法,將 Word 文檔每 N 頁拆分為單獨的文件:
使用 VBA 程式碼將 Word 文檔每 N 頁拆分為單獨的文件
使用 VBA 程式碼將 Word 文檔每 N 頁拆分為單獨的文件
要根據每 N 頁將大型文檔拆分為單獨的文件,以下 VBA 程式碼可以幫助您。請按照以下步驟操作:
- 按住 ALT + F11 打開 Microsoft Visual Basic for Applications 視窗。
- 點擊 插入 > 模組,然後將以下程式碼複製並粘貼到空白模組中:
- 粘貼程式碼後,仍在 Microsoft Visual Basic for Applications 視窗中,點擊 工具 > 引用在 引用 - 專案 對話框中,勾選 Microsoft VBScript Regular Expressions 5.5 選項,該選項位於 可用引用 列表中。參見截圖:
- 點擊 確定,然後按 F5 鍵運行程式碼。
- 將出現一個 瀏覽文件夾 對話框。選擇您要保存拆分文件的文件夾,然後點擊 確定。
- 另一個提示將出現,要求輸入您希望拆分的頁數。輸入所需的頁數並點擊 確定。
- 文檔將每 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 number of pages per 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 a valid page number:", vbInformation, "Kutools for Word"
Exit Sub
End If
If VBA.Int(xSplit) >= xPageCount Then
MsgBox "The entered number exceeds the total page count of the document." & vbCrLf & "Please enter a valid number.", 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
使用驚人的功能將 Word 文檔每 N 頁拆分為單獨的文件
Kutools for Word 提供了一個強大的 文檔拆分 功能,使您可以根據標題 1、分頁符、分節符或頁面快速將大型 Word 文檔拆分為多個單獨的文件。此功能簡化了原本繁瑣的手動過程。
- 點擊 Kutools Plus > 文檔拆分。
- 在出現的對話框中,根據需要配置以下選項:
- 從 類型 菜單中選擇 文件頁碼倍數 選項。
- 在輸入框中指定 n 值。
- 選擇保存位置。
- 為拆分的文檔指定前綴。
- 完成設置後,點擊 確定。文檔將根據所選選項(例如,每 7 頁)拆分為多個文件。
使用 Kutools for Word 中的 文檔拆分 功能,可以輕鬆高效地將大型文檔拆分為較小且易於管理的文件。
最佳辦公效率工具
Kutools for Word -讓你的 Word進階,擁有超過 100 項強大功能!
🤖 Kutools AI Features:AI Assistant / 即時助手 / 超級潤色(保留格式) / 超級翻譯(保留格式) / AI遮擋 / AI校正...
📘 文檔掌控:文檔拆分頁面 / 文檔合併 / 選取內容多格式導出(PDF/TXT/DOC/HTML...)/ 批量轉換為 PDF...
✏內容編輯:跨多個文件批量查找與替換 / 圖片大小統一調整 / 行列翻轉表格 / 表格轉文本...
🧹 潔淨無憂:一鍵清除多餘空格 / 分節符 /文字方塊 / 超鏈接 / 更多清除工具請至 清除所有 群組...
➕ 創新插入:插入千位分隔符 / 複選框 / 選項按鈕 / 二維碼 / 條形碼 / 批量插入圖片 / 更多插入功能請前往 插入 群組...
🔍 精準選取:鎖定特定頁面 / 表格 / 形狀 / 標題段落 / 使用更多 選取 功能提升導航效率...
⭐ 優質強化:輕鬆跳轉至文件夾 / 自動插入重複文字 / 切換文檔窗口 /11 項轉換工具...
🌍 支援40+ 種語言:可用您的偏好語言體驗 Kutools —— 支援英語、西班牙語、德語、法語、中文等超過40 種語言!
