![]() |
|
#1
|
|||
|
|||
|
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! |
|
#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 |
|
#3
|
|||
|
|||
|
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? |
|
#4
|
||||
|
||||
|
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 |
|
#5
|
|||
|
|||
|
gmayor, I'm sorry I wasn't able to get back to you sooner. I have recently started this project back up and I was able to make it work. I had a few errors in my bookmarks. Thank you!
I'm messing around with this a little more and I'm wondering, can I name the PDF by one of the headers within the bookmark? My understand of the code is that it: 1. Creates a path. 2. Looks for a bookmark that does not have hidden text. 3. Creates that bookmark into a PDF and names it. 4. Repeats until all non-hidden text bookmarks found. I'm thinking that if I wanted to name the file by using a heading style I'd have to plug it in within step 3. More specifically, replace oBM.Name with something that refers to a heading style .Name. However, I can't figure out what that language would be. I've tried Headerstyle and Styles but I haven't gotten it to work. Any push in the right direction would be greatly appreciated! |
|
#6
|
||||
|
||||
|
Without knowing more about your document construction and how you have used styles it is impossible to answer, but it immediately occurs to me that if you have more than one bookmark in the document, the chances are the same style names will be used resulting in duplicated names.
In any case are you sure that it is the heading style you want or the heading content. If the latter what are the style names used.
__________________
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 |
|
|
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 |
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 |