ไปยังเนื้อหาหลัก

จะแบ่งเอกสาร Word ออกเป็นไฟล์แยกกันทุกๆ 5 หรือ n หน้าได้อย่างไร?

หากคุณมีเอกสาร Word ขนาดใหญ่ซึ่งมีหลายร้อยหน้าและตอนนี้คุณต้องการแยกเอกสารนี้ออกเป็นไฟล์แยกกันทุกๆ 10 หรือ n หน้า มีวิธีที่ง่ายและรวดเร็วในการแก้ไขงานนี้โดยไม่ต้องคัดลอกและวางทีละหน้าหรือไม่?

แยกเอกสาร Word ออกเป็นไฟล์แยกกันทุกๆ 10 หรือ n หน้าด้วยรหัส VBA

แยกเอกสาร Word ออกเป็นไฟล์แยกกันทุกๆ 10 หรือ n หน้าด้วยคุณสมบัติที่น่าทึ่ง


แยกเอกสาร Word ออกเป็นไฟล์แยกกันทุกๆ 10 หรือ n หน้าด้วยรหัส VBA

หากต้องการแบ่งเอกสารขนาดใหญ่ออกเป็นไฟล์แยกตามทุกๆ 10 หรือ n หน้ารหัส VBA ต่อไปนี้สามารถช่วยคุณได้โปรดดำเนินการดังนี้:

1. กด ALT + F11 คีย์เพื่อเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

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 สำหรับแอปพลิเคชัน โปรดคลิก เครื่องมือ > อ้างอิงและในโผล่ออกมา เอกสารอ้างอิง - โครงการ กล่องโต้ตอบตรวจสอบ Microsoft VBScript นิพจน์ทั่วไป 5.5 ตัวเลือกใน อ้างอิงที่มีอยู่ กล่องรายการดูภาพหน้าจอ:

4. จากนั้นคลิก OK จากนั้นกด F5 กุญแจสำคัญในการเรียกใช้รหัสนี้และไฟล์ เรียกดูโฟลเดอร์ กล่องโต้ตอบจะปรากฏขึ้นโปรดเลือกโฟลเดอร์ที่คุณต้องการใส่ไฟล์แยกดูภาพหน้าจอ:

5. จากนั้นคลิก OK และกล่องข้อความแจ้งอีกอันจะปรากฏขึ้นเพื่อเตือนให้คุณป้อนหมายเลขหน้านับที่คุณต้องการแยกตามดูภาพหน้าจอ:

6. จากนั้นคลิก OK ปุ่มเอกสาร Word ที่ใช้งานได้ถูกแบ่งออกเป็นไฟล์แยกกันทุกๆ 10 หน้าคุณสามารถไปที่โฟลเดอร์ที่ระบุเพื่อดูผลลัพธ์:


แยกเอกสาร Word ออกเป็นไฟล์แยกกันทุกๆ 10 หรือ n หน้าด้วยคุณสมบัติที่น่าทึ่ง

Kutools สำหรับ Word รวมถึงคุณสมบัติที่มีประสิทธิภาพ - แยก ด้วยยูทิลิตี้นี้คุณสามารถแยกเอกสาร Word ขนาดใหญ่ออกเป็นไฟล์แยกกันได้อย่างรวดเร็วตาม Heading1 ตัวแบ่งหน้าตัวแบ่งส่วนและหน้า

ทิปส์:ที่จะใช้สิ่งนี้ แยก ประการแรกคุณควรดาวน์โหลดไฟล์ Kutools สำหรับ Wordแล้วใช้คุณสมบัตินี้อย่างรวดเร็วและง่ายดาย

หลังจากการติดตั้ง Kutools สำหรับ Wordโปรดทำตามนี้:

1. คลิก Kutools พลัส > แยกดูภาพหน้าจอ:

2. ในกล่องโต้ตอบที่โผล่ออกมาโปรดตั้งค่าการดำเนินการต่อไปนี้ตามที่คุณต้องการดูภาพหน้าจอ:

3. หลังจากเสร็จสิ้นการตั้งค่าโปรดคลิก OKและเอกสารทั้งหมดจะแบ่งออกเป็นหลายไฟล์ตามแต่ละหน้าโปรดดูภาพหน้าจอ:

คลิกเพื่อดาวน์โหลด Kutools for Word และทดลองใช้ฟรีทันที!

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

Kutools สำหรับ Word - ยกระดับประสบการณ์คำศัพท์ของคุณด้วย Over 100 คุณสมบัติเด่น!

🤖 Kutools ผู้ช่วย AI: แปลงโฉมงานเขียนของคุณด้วย AI - สร้างเนื้อหา  /  เขียนข้อความใหม่  /  สรุปเอกสาร  /  สอบถามข้อมูล ขึ้นอยู่กับเอกสารทั้งหมดนี้ภายใน Word

📘 ความเชี่ยวชาญด้านเอกสาร: แยกหน้า  /  ผสานเอกสาร  /  ส่งออกตัวเลือกในรูปแบบต่างๆ (PDF/TXT/DOC/HTML...)  /  แบทช์แปลงเป็น PDF  /  ส่งออกเพจเป็นรูปภาพ  /  พิมพ์หลายไฟล์พร้อมกัน...

การแก้ไขเนื้อหา: ค้นหาแบทช์และแทนที่ ข้ามหลายไฟล์  /  ปรับขนาดรูปภาพทั้งหมด  /  ย้ายแถวและคอลัมน์ของตาราง  /  แปลงตารางเป็นข้อความ...

🧹 ทำความสะอาดได้อย่างง่ายดาย: หลบไป พื้นที่พิเศษ  /  แบ่งส่วน  /  ส่วนหัวทั้งหมด  /  กล่องข้อความ  /  เชื่อมโยงหลายมิติ  / หากต้องการดูเครื่องมือถอดเพิ่มเติม โปรดไปที่ของเรา ลบกลุ่ม...

ส่วนแทรกโฆษณา: แทรก ตัวคั่นหลักพัน  /  กล่องกาเครื่องหมาย  /  ปุ่มวิทยุ  /  คิวอาร์โค้ด  /  บาร์โค้ด  /  ตารางเส้นทแยงมุม  /  คำอธิบายสมการ  /  คำบรรยายภาพ  /  คำบรรยายตาราง  /  รูปภาพหลายภาพ  / ค้นพบเพิ่มเติมใน แทรกกลุ่ม...

🔍 การเลือกที่แม่นยำ: ระบุ หน้าที่เฉพาะเจาะจง  /  ตาราง  /  รูปร่าง  /  หัวเรื่องย่อหน้า  / เสริมการนำทางด้วย ข้อมูลเพิ่มเติม เลือกคุณสมบัติ...

การปรับปรุงดาว: นำทางอย่างรวดเร็วไปยังสถานที่ใด ๆ  /  แทรกข้อความซ้ำอัตโนมัติ  /  สลับระหว่างหน้าต่างเอกสารได้อย่างราบรื่น  /  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