VBA Word, Split Word File into Multiple Files (Every X Lines)

In the article below I’ve explained how you can use VBA for word to split a word document into multiple files every X pages:

In this article I will explain how you can split the document every X lines.


Split Every X Lines:

The following code splits the document every 3 lines. The new documents are saved in the path “D:StuffBusinessTemp”. Note you must change the highlighted text to an address on your computer:

Option Explicit
Sub Example1()
'temp documents
Dim objDocument As Document
'index of the last page
Dim intLastPage As Integer
'index of the last line
Dim intLastLine As Integer
Dim flag As Boolean
'index of the current line
Dim intCurrentLine As Integer
'index of the last page
Dim intCurrentPage As Integer
'used for naming the documents upon saving
Dim intIndex As Integer
Dim intNewLine As Integer


'this can be changed
intCountLines = 3
'gets the last line and page numbers
Selection.EndKey unit:=wdStory
intLastLine = Selection.Information( _
    wdFirstCharacterLineNumber)
intLastPage = Selection.Information( _
    wdActiveEndPageNumber)
'return to the first line
Selection.HomeKey unit:=wdStory
With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, Name:="PrevBM"
    .DefaultSorting = wdSortByName
    .ShowHidden = False
End With
intIndex = 1
flag = True
'keeps going until the end of the file is reached
While flag = True
    'move down the appropriate amount of line
    Selection.MoveDown unit:=wdLine, _
        Count:=intCountLines - 1
    'go to the end of th line
    Selection.EndKey unit:=wdLine
    'create a bookmark
    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="NextBM"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
    'select between the 2 bookmarks
    Selection.Start = Bookmarks("PrevBM").End
    Selection.End = Bookmarks("NextBM").Start
       
       
    'creates a word document object and pastes the lines
    Selection.Copy
    Set objDocument = Documents.Add
    objDocument.Activate
    objDocument.Range.Paste
    objDocument.SaveAs2 ("D:StuffBusinessTemp" _
        & intIndex)
    intIndex = intIndex + 1
    objDocument.Close
    ThisDocument.Activate
    ThisDocument.Bookmarks("PrevBM").Delete
    ThisDocument.Bookmarks("NextBM").Delete
    'removes selection and moves down a line
    Selection.MoveRight unit:=wdCharacter, Count:=1
    'gets the current line number
    intCurrentLine = Selection.Information( _
        wdFirstCharacterLineNumber)
    'gets the current page number
    intCurrentPage = Selection.Information( _
        wdActiveEndPageNumber)
    'checks if the current page and line number are
    'the last page and line number
    If intCurrentPage = intLastPage Then
        If intCurrentLine = intLastLine Then
           flag = False
        End If
    End If

    Selection.MoveDown unit:=wdLine, _
        Count:=1
    With ActiveDocument.Bookmarks
        .Add Range:=Selection.Range, Name:="PrevBM"
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
Wend

End Sub

This was the data in the word document:

Word, Data
These are the new documents created it the path “D:StuffBusinessTemp”:

Word, VBA, New Documents

Below you can see the data in some of the documents:

Data From Word Documents


Program Algorithm:

The program follows the steps below:

Step 1: Get the last page and line number

Step 2: Create bookmark1

Step 3: Move down 3 lines

Step 4: Create bookmark2

Step 5: Select the text between the two bookmarks

Step 6: Copy the selected text

Step 7: Create a new word document. Paste the selected text onto it. Save and close it.

Step 8: Delete the two bookmarks

Step 9: Move one line down

Step 10: Create bookmark 1

Step 11: Get the current line and page number

Step 12: Compare the values from step 1 with the values from step 11. If they match exit the program. Else goto step 2.


Code Explanation:

This section shows what each part of the code above is doing. In the previous section you can see what each step refers to. The line below determines X (the document is split every X lines):

'this can be changed
intCountLines = 3

Step 1:

Selection.EndKey unit:=wdStory
intLastLine = Selection.Information( _
    wdFirstCharacterLineNumber)
intLastPage = Selection.Information( _
    wdActiveEndPageNumber)

Step 2:

With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, Name:="PrevBM"
    .DefaultSorting = wdSortByName
    .ShowHidden = False
End With

Step 3:

Selection.MoveDown unit:=wdLine, _
    Count:=intCountLines - 1

Step 4:

With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, Name:="NextBM"
    .DefaultSorting = wdSortByName
    .ShowHidden = False
End With

Step 5:

Selection.Start = Bookmarks("PrevBM").End
Selection.End = Bookmarks("NextBM").Start

Step 6:

Selection.Copy

Step 7:

Set objDocument = Documents.Add
objDocument.Activate
objDocument.Range.Paste
objDocument.SaveAs2 ("D:StuffBusinessTemp" _
    & intIndex)
intIndex = intIndex + 1
objDocument.Close

Step 8:

ThisDocument.Bookmarks("PrevBM").Delete
ThisDocument.Bookmarks("NextBM").Delete

Step 9:

Selection.MoveRight unit:=wdCharacter, Count:=1
Selection.MoveDown unit:=wdLine, _
    Count:=1

Step 10:

With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, Name:="PrevBM"
    .DefaultSorting = wdSortByName
    .ShowHidden = False
End With

Step 11:

intCurrentLine = Selection.Information( _
    wdFirstCharacterLineNumber)
'gets the current page number
intCurrentPage = Selection.Information( _
    wdActiveEndPageNumber)

Step 12:

If intCurrentPage = intLastPage Then
    If intCurrentLine = intLastLine Then
        flag = False
    End If
End If

You can download the file and code related to this article from the link below:

See also:

If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website  www.software-solutions-online.com

One thought on “VBA Word, Split Word File into Multiple Files (Every X Lines)”

  1. Afzal says:

    Hi my friend,
    Could u pls help me to split ever 6 pages to a single document? In the word using vba coding, your help will be really appreciated.. Kindly pls mail me the code if u can…pls this would save my whole time

Leave a Reply

Your email address will not be published. Required fields are marked *