![]() |
#2
|
||||
|
||||
![]()
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 |
Tags |
print command, vba code |
|
![]() |
||||
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 |
![]() |
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 |