Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #4  
Old 10-05-2018, 06:01 AM
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,142
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

That's what happens when you only have half the required information to work with.

You will need to create a temporary document based on the original if you want the headers and footers. The path issue is addressed and the naming issue should be, thus the following will get you closer, but without access to the original document it is still based on guesswork.

There should be no bookmark at the end of the original document and no manual or section page breaks in that document.

If the document is the result of a mail merge use http://www.gmayor.com/MergeAndSplit.htm instead




Code:
Sub Splitter2()
' 'Graham Mayor - http://www.gmayor.com - Last updated - 05 Oct 2018
' to split a document to separate PDF files by bookmarks
Dim strMask As String
Dim lngDocs As Long
Dim lngCount As Long
Dim strName As String
Dim oDoc As Document
Dim oTempDoc As Document
Dim oRng As Range
Dim strPath As String

    Set oDoc = ActiveDocument
    oDoc.Save
    strPath = oDoc.path & "\"
    lngDocs = oDoc.Bookmarks.Count + 1
    lngCount = 1
    While lngCount <= lngDocs
        Set oRng = oDoc.Range
        If lngCount < lngDocs Then
            oRng.End = oDoc.Bookmarks(1).Range.End
            strMask = oDoc.Bookmarks(1).Name
            oDoc.Bookmarks(1).Delete
        End If

        Set oTempDoc = Documents.Add(oDoc.FullName)
        oTempDoc.Range.FormattedText = oRng.FormattedText

        strName = strMask & " " & LTrim$(Str$(lngCount)) & ".pdf"
        oTempDoc.ExportAsFixedFormat _
                OutputFileName:=strPath & strName, _
                ExportFormat:=wdExportFormatPDF, _
                OptimizeFor:=wdExportOptimizeForPrint, _
                CreateBookmarks:=wdExportCreateWordBookmarks, _
                DocStructureTags:=True, _
                BitmapMissingFonts:=True
        lngCount = lngCount + 1
        oRng.Text = ""
        oTempDoc.Close wdDoNotSaveChanges
    Wend
    oDoc.Close wdDoNotSaveChanges
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:50 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