Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #6  
Old 10-05-2018, 10:07 PM
gmayor's Avatar
gmayor gmayor is offline Split word file into several PDF using the bookmarks as split positions and name Windows 10 Split word file into several PDF using the bookmarks as split positions and name Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Your sample document does not match the criteria required by the macro(s) I posted.

You have the bookmarks at the starts of the segments and not at the ends, and you have a column break(?) to separate each page, which was completely unexpected. To deal with that sort of layout you need a different approach.

Provided your actual document reflects the sample, the following will work. The sample creates four PDF files named with the bookmark names in the same folder as the document.

Code:
Sub Splitter3()
' 'Graham Mayor - http://www.gmayor.com - Last updated - 06 Oct 2018
' to split example document to separate PDF files
Dim strName As String
Dim oDoc As Document
Dim oTempDoc As Document
Dim oRng As Range, oPage As Range
Dim strPath As String

    Set oDoc = ActiveDocument
    oDoc.Save
    strPath = oDoc.path & "\"
    Set oPage = oDoc.Range
    With oPage.Find
        Do While .Execute(FindText:="^n")
            Set oRng = oDoc.Range
            oRng.End = oPage.End
            oPage.Text = ""
            Set oTempDoc = Documents.Add(oDoc.FullName)
            oTempDoc.Range.FormattedText = oRng.FormattedText

            strName = oTempDoc.Bookmarks(1).Name & ".pdf"
            oTempDoc.ExportAsFixedFormat _
                    OutputFileName:=strPath & strName, _
                    ExportFormat:=wdExportFormatPDF, _
                    OptimizeFor:=wdExportOptimizeForPrint, _
                    CreateBookmarks:=wdExportCreateWordBookmarks, _
                    DocStructureTags:=True, _
                    BitmapMissingFonts:=True
            oRng.Text = ""
            oTempDoc.Close wdDoNotSaveChanges
        Loop
    End With
    strName = oDoc.Bookmarks(1).Name & ".pdf"
    oDoc.ExportAsFixedFormat _
            OutputFileName:=strPath & strName, _
            ExportFormat:=wdExportFormatPDF, _
            OptimizeFor:=wdExportOptimizeForPrint, _
            CreateBookmarks:=wdExportCreateWordBookmarks, _
            DocStructureTags:=True, _
            BitmapMissingFonts:=True
    oDoc.Close wdDoNotSaveChanges
lbl_Exit:
    Set oDoc = Nothing
    Set oTempDoc = Nothing
    Set oRng = Nothing
    Set oPage = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Split word file into several PDF using the bookmarks as split positions and name Word VBA - Split Document By Headings - Save File Name As Heading Name jc491 Word VBA 7 01-21-2022 11:04 AM
Split word file into several PDF using the bookmarks as split positions and name split word document based on bookmarks with each new document title of the bookmark megatronixs Word VBA 9 09-05-2020 02:29 PM
Split function in Excel (split the screen) Officer_Bierschnitt Excel 1 07-05-2017 07:02 AM
Split word file into several PDF using the bookmarks as split positions and name Programmatically split a rtf word file without opening Esgrimidor Word VBA 8 03-29-2017 11:15 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:08 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft