To Separate pages a word File Using Word VBA

Priyantha
StarLounger
Posts: 93
Joined: 10 Oct 2022, 02:52

To Separate pages a word File Using Word VBA

Post by Priyantha »

Dear All,

I use the attached VBA code below to separate a Word document that includes many pages & to convert each separated file into a PDF file. The pages are separated based on the word "Grand Total" at the end of each page and each PDF file is named its "Third-party Code" (e.g. 2100-004.pdf). Even though this code works correctly, an error occurs when separating the last page.

For example, the ''2100-007'.pdf'' file which is defined according to the Third-party Code on page 04 contains the information on page 05. The ''2100-007.pdf'' file should be prepared based on the information on page no.04 and no pdf file is required for page no. 05.

Please help me to solve this issue.

Thanks,

Priyantha
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: To Separate pages a word File Using Word VBA

Post by HansV »

That is a really horrible Word document. This is the best I can do. I'll leave it to you to clean up the documents before saving as PDF.

Code: Select all

Sub Test()
    Dim doc As Document
    Dim rng As Range
    Dim pdf As Document
    Dim i As Long
    Dim j As Long
    Dim fn As String
    ' Open document
    With Application.FileDialog(msoFileDialogOpen)
        If .Show Then
            Set doc = Documents.Open(.SelectedItems(1))
        Else
            Beep
            Exit Sub
        End If
    End With
    ' Loop through the sections of the document
    Application.ScreenUpdating = False
    For i = 2 To doc.Sections.Count
        Set rng = doc.Sections(i).Range
        ' Remove section break
        rng.MoveEnd Count:=-1
        ' Copy the section
        rng.Copy
        ' Create a new document
        Set pdf = Documents.Add
        ' Paste to the new document
        pdf.Content.Paste
        ' Loop through the frames of the section
        For j = 1 To pdf.Frames.Count
            ' Find "Third Party Code"
            If pdf.Frames(j).Range.Text Like "Third Party Code*" Then
                ' Get the text from the next frame
                fn = pdf.Frames(j + 1).Range.Text
                ' Remove the paragraph mark at the end
                'fn = Left(fn, Len(fn) - 1)
                ' Save the new document
                pdf.ExportAsFixedFormat OutputFileName:=fn & ".pdf", ExportFormat:=wdExportFormatPDF
                ' Exit the inner loop
                Exit For
            End If
        Next j
        ' Close the new document
        pdf.Close SaveChanges:=False
    Next i
    ' Close Word document
    doc.Close SaveChanges:=False
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

Priyantha
StarLounger
Posts: 93
Joined: 10 Oct 2022, 02:52

Re: To Separate pages a word File Using Word VBA

Post by Priyantha »

Dear Hans,

Thanks for your guidance.

BR,

Priyantha.