Home > Blockchain >  Spliting a word document by pages and saving each as a line from that page
Spliting a word document by pages and saving each as a line from that page

Time:01-27

I have used mail merge to pull data from excel into a word document, I have then used finish amd merge, edit individual docs, all. On the created doc I want to split this in to separate docs by page and save using the first line on each of the respective pages.

It does split the document into the desired multi doc format however the issue is I want it to save using the top line off each word (persons name) currently it saves as Docname_0001, Docname_0002, Docname_0003 ect.

Code below any help would be great

Sub SplitIntoPages()
Dim docMultiple As Document
Dim docSingle As Document
Dim rngPage As Range
Dim iCurrentPage As Integer
Dim iPageCount As Integer
Dim strNewFileName As String
Application.ScreenUpdating = False 'Makes the code run faster and reduces screen _
flicker a bit.
Set docMultiple = ActiveDocument 'Work on the active document _
(the one currently containing the Selection)
Set rngPage = docMultiple.Range 'instantiate the range object
iCurrentPage = 1
'get the document's page count
iPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages)
Do Until iCurrentPage > iPageCount
If iCurrentPage = iPageCount Then
rngPage.End = ActiveDocument.Range.End 'last page (there won't be a next page)
Else
'Find the beginning of the next page
'Must use the Selection object. The Range.Goto method will not work on a page
Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage   1
'Set the end of the range to the point between the pages
rngPage.End = Selection.Start
End If
rngPage.Copy 'copy the page into the Windows clipboard
Set docSingle = Documents.Add 'create a new document
docSingle.Range.PasteAndFormat (wdFormatOriginalFormatting)
'paste the clipboard contents to the new document
'remove any manual page break to prevent a second blank
docSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:=""
'build a new sequentially-numbered file name based on the original multi-paged file name and path
strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")
docSingle.SaveAs strNewFileName 'save the new single-paged document
iCurrentPage = iCurrentPage   1 'move to the next page
docSingle.Close 'close the new document
rngPage.Collapse wdCollapseEnd 'go to the next page
Loop 'go to the top of the do loop
Application.ScreenUpdating = True 'restore the screen updating
'Destroy the objects.
Set docMultiple = Nothing
Set docSingle = Nothing
Set rngPage = Nothing
End Sub

I have tried amending this section

strNewFileName = Replace(docMultiple.FullName, ".doc", "_" & Right$("000" & iCurrentPage, 4) & ".doc")
docSingle.SaveAs strNewFileName 'save the new single-paged document
iCurrentPage = iCurrentPage   1 'move to the next page
docSingle.Close

I amended it to this

strNewFileName = strNewFileName & Left(docSingle.Range.Paragraphs(1), 
Len(docSingle.Range.Paragraphs(1).Range.Text) - 1)
docSingle.SaveAs strNewFileName 'save the new single-paged document

This did pull the first line I wanted, however on the following docs it added all the first lines off all the pages before it as appose to the just the first line of that page.

CodePudding user response:

strNewFileName = strNewFileName & Left(docSingle.Range.Paragraphs(1), 
Len(docSingle.Range.Paragraphs(1).Range.Text) - 1)

This adds the new value to the existing value of strNewFileName with each iteration of the loop.

Sub SplitIntoPages()
    Dim docMultiple As Document
    Dim docSingle As Document
    Dim rngPage As Range
    Dim iCurrentPage As Integer
    Dim iPageCount As Integer
    Dim strNewFileName As String, filePath As String
    Application.ScreenUpdating = False 'Makes the code run faster and reduces screen _
                                        flicker a bit.
    Set docMultiple = ActiveDocument 'Work on the active document _
                                      (the one currently containing the Selection)
    filePath = docMultiple.Path & Application.PathSeparator
    Set rngPage = docMultiple.Range 'instantiate the range object
    iCurrentPage = 1
    'get the document's page count
    iPageCount = docMultiple.Content.ComputeStatistics(wdStatisticPages)
    Do Until iCurrentPage > iPageCount
        If iCurrentPage = iPageCount Then
            rngPage.End = ActiveDocument.Range.End 'last page (there won't be a next page)
        Else
            'Find the beginning of the next page
            'Must use the Selection object. The Range.Goto method will not work on a page
            Selection.GoTo wdGoToPage, wdGoToAbsolute, iCurrentPage   1
            'Set the end of the range to the point between the pages
            rngPage.End = Selection.Start
        End If
        Set docSingle = Documents.Add 'create a new document
        'set text of new document to same as rngPage
        docSingle.Range.FormattedText = rngPage.FormattedText
        'remove any manual page break to prevent a second blank
        docSingle.Range.Find.Execute Findtext:="^m", ReplaceWith:=""
        
        strNewFileName = Left(docSingle.Paragraphs(1).Range.Text, Len(docSingle.Paragraphs(1).Range.Text) - 1) & ".doc"
        docSingle.SaveAs filePath & strNewFileName   'save the new single-paged document
        iCurrentPage = iCurrentPage   1 'move to the next page
        docSingle.Close 'close the new document
        rngPage.Collapse wdCollapseEnd 'go to the next page
    Loop 'go to the top of the do loop
    Application.ScreenUpdating = True 'restore the screen updating
    'Destroy the objects.
    Set docMultiple = Nothing
    Set docSingle = Nothing
    Set rngPage = Nothing
End Sub
  • Related