Microsoft Office Forums How to print bookmarks in a document to separate .pdf file

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-11-2019, 06:58 AM
timf timf is offline How to print bookmarks in a document to separate .pdf file Windows 10 How to print bookmarks in a document to separate .pdf file Office 2019
Novice
How to print bookmarks in a document to separate .pdf file
 
Join Date: Oct 2019
Posts: 2
timf is on a distinguished road
Default How to print bookmarks in a document to separate .pdf file

I'm new to using VBA (and coding) and I'm not even sure this is possible but here's what I have and what I'm trying to do.

I have a large word doc that contains different sections. Each section was a separate file that has been inserted into this document. I've turned each section into a bookmark and attached the bookmark to a checkbox. This way someone can uncheck a box if they don't want that section included in the document. The checkboxes work using a hidden font command. This whole setup is much easier because some sections need edited by the user and with everything in one place it goes faster.



So what I need to do is be able to print to pdf each included bookmark as a file in a specific folder. For instance, when I click print, I need the bookmark "Section 1" to print to pdf, call itself "Section 1", and go to a folder called "All Sections for this Project". I also need the unchecked sections to NOT print but I think I could do that using an if then command. Please let me know if that is clear or if I need to explain more.

If someone could let me know if this is possible and help out a bit that'd be great!
Reply With Quote
  #2  
Old 10-11-2019, 11:08 PM
gmayor's Avatar
gmayor gmayor is offline How to print bookmarks in a document to separate .pdf file Windows 10 How to print bookmarks in a document to separate .pdf file Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,990
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

It is fairly simple to print visible bookmarked texts as separate PDFs.
The code includes two additional standard utilities to ensure that the bookmark names contain no illegal filename characters and the Path exists. Change the path as appropriate

Code:
Option Explicit

Sub SaveBookmarksAsPDF()
Dim oBM As Bookmark
Dim oRng As Range
Dim strFname As String
Const strPath As String = "D:\All Sections for this Project\"
    CreateFolders strPath
    For Each oBM In ActiveDocument.Bookmarks
        Set oRng = oBM.Range
        If Not oRng.Font.Hidden Then
            strFname = CleanFilename(oBM.Name & ".pdf")
            oRng.ExportAsFixedFormat OutputFilename:=strPath & strFname, _
                                     ExportFormat:=wdExportFormatPDF, _
                                     OpenAfterExport:=False
        End If
    Next oBM
lbl_Exit:
    Set oBM = Nothing
    Set oRng = Nothing
    Exit Sub
End Sub

Private Sub CreateFolders(strPath As String)
'A Graham Mayor/Greg Maxey AddIn Utility Macro
Dim oFSO As Object
Dim lng_PathSep As Long
Dim lng_PS As Long
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
    lng_PathSep = InStr(3, strPath, "\")
    If lng_PathSep = 0 Then GoTo lbl_Exit
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Do
        lng_PS = lng_PathSep
        lng_PathSep = InStr(lng_PS + 1, strPath, "\")
        If lng_PathSep = 0 Then Exit Do
        If Len(Dir(Left(strPath, lng_PathSep), vbDirectory)) = 0 Then Exit Do
    Loop
    Do Until lng_PathSep = 0
        If Not oFSO.FolderExists(Left(strPath, lng_PathSep)) Then
            oFSO.CreateFolder Left(strPath, lng_PathSep)
        End If
        lng_PS = lng_PathSep
        lng_PathSep = InStr(lng_PS + 1, strPath, "\")
    Loop
lbl_Exit:
    Set oFSO = Nothing
    Exit Sub
End Sub

Private Function CleanFilename(strFileName As String) As String
Dim arrInvalid() As String
Dim lng_Index As Long
    'Define illegal characters (by ASCII CharNum)
    arrInvalid = Split("9|10|11|13|34|42|47|58|60|62|63|92|124", "|")
    'Remove any illegal filename characters
    CleanFilename = strFileName
    For lng_Index = 0 To UBound(arrInvalid)
        CleanFilename = Replace(CleanFilename, Chr(arrInvalid(lng_Index)), Chr(95))
    Next lng_Index
lbl_Exit:
    Exit Function
End Function
__________________
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
  #3  
Old 10-18-2019, 01:49 PM
timf timf is offline How to print bookmarks in a document to separate .pdf file Windows 10 How to print bookmarks in a document to separate .pdf file Office 2019
Novice
How to print bookmarks in a document to separate .pdf file
 
Join Date: Oct 2019
Posts: 2
timf is on a distinguished road
Default

Thank you! I have been testing the code out and I'm running into an issue that I haven't been able to fix.

When I run the macro, the bookmarks go into the file called All Sections for this Project. However, ALL of the bookmarks go into the file, including those that have hidden font. I tried altering the If Not statement but haven't had any success.

Any suggestions?
Reply With Quote
  #4  
Old 10-18-2019, 08:17 PM
gmayor's Avatar
gmayor gmayor is offline How to print bookmarks in a document to separate .pdf file Windows 10 How to print bookmarks in a document to separate .pdf file Office 2016
Expert
 
Join Date: Aug 2014
Posts: 2,990
gmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nicegmayor is just really nice
Default

There should be a separate PDF file for each non-hidden bookmark only in the named folder. Did you change the path as required. The original code created the folder on the D drive. Clearly it can't do that if you don't have a D drive.

Can you post a sample document that doesn't work as intended?
__________________
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
Reply

Tags
print command, vba code

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word document with VBA and Form Fields reset when select File,Print fatal.lordes Word VBA 9 04-27-2018 11:30 PM
Hyperlinking to Bookmarks in a separate Word document Cat Perry Word 7 03-23-2018 04:39 PM
How to print bookmarks in a document to separate .pdf file Tweak Macro to Save Each Page of Word Document as Separate PDF or Doc File? Hewg74 Word VBA 3 08-22-2016 05:20 PM
Any easy way to separate a Word document into separate files? SamHelm Word 0 08-21-2010 05:29 AM
print file is 14MB when document is only 850kb pompeydave PowerPoint 0 06-01-2009 05:58 PM


All times are GMT -7. The time now is 06:17 PM.


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