Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-17-2016, 08:46 AM
Albus Albus is offline Save as PDF with bookmarks Windows 7 64bit Save as PDF with bookmarks Office 2010 64bit
Novice
Save as PDF with bookmarks
 
Join Date: Dec 2014
Posts: 12
Albus is on a distinguished road
Default Save as PDF with bookmarks

Hello,

These are the requirements for my macro:
1. Save the current document in the same folder with the same document name, except it will end in .pdf.
2. The "create bookmarks from headers" option is enabled.
3. It will prompt me if there is a file with the current name already.

I have this code, which accomplishes 1 and 3, but how would I add to it so that "create bookmarks from headers" is always enabled:



Code:
    Sub Save_to_PDF()
    
      With Dialogs(wdDialogFileSaveAs)
            .Format = wdFormatPDF
            .Show
         End With
    
    End Sub
Thanks for your help!
Reply With Quote
  #2  
Old 08-18-2016, 09:26 PM
gmayor's Avatar
gmayor gmayor is offline Save as PDF with bookmarks Windows 10 Save as PDF with bookmarks Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
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 of
Default

Use instead the following. You will have to supply the name and path (here - strDocName).

Code:
ActiveDocument.ExportAsFixedFormat OutputFilename:=strDocName, _
                                       ExportFormat:=wdExportFormatPDF, _
                                       OpenAfterExport:=False, _
                                       OptimizeFor:=wdExportOptimizeForPrint, _
                                       Range:=wdExportAllDocument, From:=1, to:=1, _
                                       Item:=wdExportDocumentContent, _
                                       IncludeDocProps:=True, _
                                       KeepIRM:=True, _
                                       CreateBookmarks:=wdExportCreateHeadingBookmarks, _
                                       DocStructureTags:=True, _
                                       BitmapMissingFonts:=True, _
                                       UseISO19005_1:=False
__________________
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 08-18-2016, 09:33 PM
Albus Albus is offline Save as PDF with bookmarks Windows 7 64bit Save as PDF with bookmarks Office 2010 64bit
Novice
Save as PDF with bookmarks
 
Join Date: Dec 2014
Posts: 12
Albus is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Use instead the following. You will have to supply the name and path (here - strDocName).

Code:
ActiveDocument.ExportAsFixedFormat OutputFilename:=strDocName, _
                                       ExportFormat:=wdExportFormatPDF, _
                                       OpenAfterExport:=False, _
                                       OptimizeFor:=wdExportOptimizeForPrint, _
                                       Range:=wdExportAllDocument, From:=1, to:=1, _
                                       Item:=wdExportDocumentContent, _
                                       IncludeDocProps:=True, _
                                       KeepIRM:=True, _
                                       CreateBookmarks:=wdExportCreateHeadingBookmarks, _
                                       DocStructureTags:=True, _
                                       BitmapMissingFonts:=True, _
                                       UseISO19005_1:=False
Hey thanks for the reply. I don't have access to test but do you know if this will prompt me to choose if I want to overwrite a file that already exists with the same name?
Reply With Quote
  #4  
Old 08-19-2016, 09:00 PM
gmayor's Avatar
gmayor gmayor is offline Save as PDF with bookmarks Windows 10 Save as PDF with bookmarks Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
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 of
Default

No, it will overwrite the existing file. If you want a prompt you will need extra code and you will have to determine what to do in that eventuality. My recommendation is to append a number to the file name, which is what the following will do:

Code:
Sub Macro1()
Dim strPath As String
Dim strDocname As String
Dim lngAsk As Long
    strPath = "C:\Path\"
    strDocname = "MyPDF.pdf"
    If FileExists(strPath & strDocname) Then
        lngAsk = MsgBox(strPath & strDocname & " already exists. Do you wish to overwrite the file?", vbYesNo)
        If Not lngAsk = vbYes Then
            strDocname = strPath & FileNameUnique(strPath, strDocname, "pdf")
        Else
            strDocname = strPath & strDocname
        End If
    Else
        strDocname = strPath & strDocname
    End If
    ActiveDocument.ExportAsFixedFormat OutputFilename:=strDocname, _
                                       ExportFormat:=wdExportFormatPDF, _
                                       OpenAfterExport:=False, _
                                       OptimizeFor:=wdExportOptimizeForPrint, _
                                       Range:=wdExportAllDocument, From:=1, to:=1, _
                                       Item:=wdExportDocumentContent, _
                                       IncludeDocProps:=True, _
                                       KeepIRM:=True, _
                                       CreateBookmarks:=wdExportCreateHeadingBookmarks, _
                                       DocStructureTags:=True, _
                                       BitmapMissingFonts:=True, _
                                       UseISO19005_1:=False
End Sub

Public Function FileNameUnique(strPath As String, _
                               strFileName As String, _
                               strExtension As String) As String
'Graham Mayor - http://www.gmayor.com
'Requires the use of the FileExists function
'strPath is the path in which the file is to be saved
'strFilename is the filename to check
'strextension is the extension of the filename to check
Dim lngF As Long
Dim lngName As Long
    strExtension = Replace(strExtension, Chr(46), "")
    lngF = 1
    lngName = Len(strFileName) - (Len(strExtension) + 1)
    strFileName = Left(strFileName, lngName)
    'If the filename exists, add or increment a number to the filename
    'and keep checking until a unique name is found
    Do While FileExists(strPath & strFileName & Chr(46) & strExtension) = True
        strFileName = Left(strFileName, lngName) & "(" & lngF & ")"
        lngF = lngF + 1
    Loop
    'Reassemble the filename
    FileNameUnique = strFileName & Chr(46) & strExtension
lbl_Exit:
    Exit Function
End Function

Public Function FileExists(strFullName As String) As Boolean
'Graham Mayor - http://www.gmayor.com
'strFullName is the name with path of the file to check
Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FileExists(strFullName) Then
        FileExists = True
    Else
        FileExists = False
    End If
lbl_Exit:
    Set FSO = Nothing
    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
  #5  
Old 08-20-2016, 05:57 AM
Albus Albus is offline Save as PDF with bookmarks Windows 7 64bit Save as PDF with bookmarks Office 2010 64bit
Novice
Save as PDF with bookmarks
 
Join Date: Dec 2014
Posts: 12
Albus is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
No, it will overwrite the existing file. If you want a prompt you will need extra code and you will have to determine what to do in that eventuality. My recommendation is to append a number to the file name, which is what the following will do:

Code:
Sub Macro1()
Dim strPath As String
Dim strDocname As String
Dim lngAsk As Long
    strPath = "C:\Path\"
    strDocname = "MyPDF.pdf"
    If FileExists(strPath & strDocname) Then
        lngAsk = MsgBox(strPath & strDocname & " already exists. Do you wish to overwrite the file?", vbYesNo)
        If Not lngAsk = vbYes Then
            strDocname = strPath & FileNameUnique(strPath, strDocname, "pdf")
        Else
            strDocname = strPath & strDocname
        End If
    Else
        strDocname = strPath & strDocname
    End If
    ActiveDocument.ExportAsFixedFormat OutputFilename:=strDocname, _
                                       ExportFormat:=wdExportFormatPDF, _
                                       OpenAfterExport:=False, _
                                       OptimizeFor:=wdExportOptimizeForPrint, _
                                       Range:=wdExportAllDocument, From:=1, to:=1, _
                                       Item:=wdExportDocumentContent, _
                                       IncludeDocProps:=True, _
                                       KeepIRM:=True, _
                                       CreateBookmarks:=wdExportCreateHeadingBookmarks, _
                                       DocStructureTags:=True, _
                                       BitmapMissingFonts:=True, _
                                       UseISO19005_1:=False
End Sub

Public Function FileNameUnique(strPath As String, _
                               strFileName As String, _
                               strExtension As String) As String
'Graham Mayor - http://www.gmayor.com
'Requires the use of the FileExists function
'strPath is the path in which the file is to be saved
'strFilename is the filename to check
'strextension is the extension of the filename to check
Dim lngF As Long
Dim lngName As Long
    strExtension = Replace(strExtension, Chr(46), "")
    lngF = 1
    lngName = Len(strFileName) - (Len(strExtension) + 1)
    strFileName = Left(strFileName, lngName)
    'If the filename exists, add or increment a number to the filename
    'and keep checking until a unique name is found
    Do While FileExists(strPath & strFileName & Chr(46) & strExtension) = True
        strFileName = Left(strFileName, lngName) & "(" & lngF & ")"
        lngF = lngF + 1
    Loop
    'Reassemble the filename
    FileNameUnique = strFileName & Chr(46) & strExtension
lbl_Exit:
    Exit Function
End Function

Public Function FileExists(strFullName As String) As Boolean
'Graham Mayor - http://www.gmayor.com
'strFullName is the name with path of the file to check
Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FileExists(strFullName) Then
        FileExists = True
    Else
        FileExists = False
    End If
lbl_Exit:
    Set FSO = Nothing
    Exit Function
End Function
Wow thanks so much for such a detailed response. Will test it out on Monday and see how she works.
Reply With Quote
  #6  
Old 08-23-2016, 06:03 AM
Albus Albus is offline Save as PDF with bookmarks Windows 7 64bit Save as PDF with bookmarks Office 2010 64bit
Novice
Save as PDF with bookmarks
 
Join Date: Dec 2014
Posts: 12
Albus is on a distinguished road
Default

Awesome, this is so close to what I need.

How would I change it slightly so that instead of a fixed path and doc name, instead it opens up the dialogue box to ask if I want to save the PDF in the current folder and also suggests the pdf document name to be the same as the original doc but ending in .pdf instead of .docx?
Reply With Quote
  #7  
Old 08-24-2016, 01:18 AM
gmayor's Avatar
gmayor gmayor is offline Save as PDF with bookmarks Windows 10 Save as PDF with bookmarks Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
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 of
Default

You need another function to get the folder and some more code in the main macro

Code:
Option Explicit

Sub Macro1()
Dim strPath As String
Dim strDocname As String
Dim lngAsk As Long
    On Error Resume Next
    ActiveDocument.Save
    If Len(ActiveDocument.Path) = 0 Then
        MsgBox "You must save the file before using this macro!"
        GoTo lbl_Exit
    End If
    On Error GoTo 0
    strPath = ActiveDocument.Path & Chr(92)
    lngAsk = MsgBox("Do you wish to save the PDF in the same folder as the document?", vbYesNo)
    If lngAsk = vbNo Then
        strPath = BrowseForFolder("Select the folder to save the PDF")
    End If
    strDocname = ActiveDocument.Name
    strDocname = Left(strDocname, InStrRev(strDocname, Chr(46))) & "pdf"
    If FileExists(strPath & strDocname) Then
        lngAsk = MsgBox(strPath & strDocname & " already exists." & vbCr & _
                        "Do you wish to overwrite the file?", vbYesNo)
        If Not lngAsk = vbYes Then
            strDocname = strPath & FileNameUnique(strPath, strDocname, "pdf")
        Else
            strDocname = strPath & strDocname
        End If
    Else
        strDocname = strPath & strDocname
    End If
    ActiveDocument.ExportAsFixedFormat OutputFilename:=strDocname, _
                                       ExportFormat:=wdExportFormatPDF, _
                                       OpenAfterExport:=False, _
                                       OptimizeFor:=wdExportOptimizeForPrint, _
                                       Range:=wdExportAllDocument, From:=1, to:=1, _
                                       Item:=wdExportDocumentContent, _
                                       IncludeDocProps:=True, _
                                       KeepIRM:=True, _
                                       CreateBookmarks:=wdExportCreateHeadingBookmarks, _
                                       DocStructureTags:=True, _
                                       BitmapMissingFonts:=True, _
                                       UseISO19005_1:=False
lbl_Exit:
    Exit Sub
End Sub

Private Function FileNameUnique(strPath As String, _
                               strFileName As String, _
                               strExtension As String) As String
'Graham Mayor - http://www.gmayor.com
'Requires the use of the FileExists function
'strPath is the path in which the file is to be saved
'strFilename is the filename to check
'strextension is the extension of the filename to check
Dim lngF As Long
Dim lngName As Long
    strExtension = Replace(strExtension, Chr(46), "")
    lngF = 1
    lngName = Len(strFileName) - (Len(strExtension) + 1)
    strFileName = Left(strFileName, lngName)
    'If the filename exists, add or increment a number to the filename
    'and keep checking until a unique name is found
    Do While FileExists(strPath & strFileName & Chr(46) & strExtension) = True
        strFileName = Left(strFileName, lngName) & "(" & lngF & ")"
        lngF = lngF + 1
    Loop
    'Reassemble the filename
    FileNameUnique = strFileName & Chr(46) & strExtension
lbl_Exit:
    Exit Function
End Function

Private Function FileExists(strFullName As String) As Boolean
'Graham Mayor - http://www.gmayor.com
'strFullName is the name with path of the file to check
Dim FSO As Object
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If FSO.FileExists(strFullName) Then
        FileExists = True
    Else
        FileExists = False
    End If
lbl_Exit:
    Set FSO = Nothing
    Exit Function
End Function

Private Function BrowseForFolder(Optional strTitle As String) As String
'Graham Mayor - http://www.gmayor.com
'strTitle is the title of the dialog box
Dim fDialog As FileDialog
    On Error GoTo err_handler
    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With fDialog
        .Title = strTitle
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then GoTo err_handler:
        BrowseForFolder = fDialog.SelectedItems.Item(1) & Chr(92)
    End With
lbl_Exit:
    Exit Function
err_handler:
    BrowseForFolder = vbNullString
    Resume lbl_Exit
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
  #8  
Old 08-25-2016, 12:13 PM
Albus Albus is offline Save as PDF with bookmarks Windows 7 64bit Save as PDF with bookmarks Office 2010 64bit
Novice
Save as PDF with bookmarks
 
Join Date: Dec 2014
Posts: 12
Albus is on a distinguished road
Default

That is perfect, thank you! I really appreciate you updating your answers after my additional questions also. Cheers
Reply With Quote
  #9  
Old 01-13-2017, 10:42 AM
rpatchet rpatchet is offline Save as PDF with bookmarks Windows 7 64bit Save as PDF with bookmarks Office 2010 64bit
Novice
 
Join Date: Jan 2017
Posts: 1
rpatchet is on a distinguished road
Default

I love your macro, I have been trying to do this for years on a huge Word document. And it worked great the first three times I used it. But the next day, the ExportAsFixedFormat fails with a read-only error. Running attrib from cmd.exe shows only the archive attribute set.

If I use ActiveDocument.SaveAs FileFormat:=wdFormatPDF (which does not let me select the PDF parameters, so it is unacceptable), the command fails and the VBA error dialog simply states that the command failed.

If I test for the existence of the file and try to delete it, the delete command fails and the VBA error dialog shows a permissions error.

I rebooted the computer to ensure no process had it locked. The Word document, macro, and PDF all exist in the same user account. When I do a File > Save As > PDF, Word prompts me to overwrite and then successfully writes the PDF.

I also encountered an odd problem with this macro. If I import the macro into a macro-free file, .docx, and run the macro, it works fine. If I save the document as a macro-enabled file, .docm, and run the macro, it fails.

Even more interesting: I deleted the PDF from the directory before I ran the macro and I still received the error "Run-time error '-2147467259 (80004005)': This file is read-only." I have no clue what file it thinks is read-only.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bookmarks Schildkröte Word VBA 8 06-28-2015 11:13 AM
Save as PDF with bookmarks Form updating Bookmarks - writes to the bookmarks multiple times PeterPlys Word VBA 13 01-14-2015 06:41 AM
Can't See Bookmarks bobmayo Word 21 06-04-2013 07:37 AM
Save as PDF with bookmarks When I try to save an existing word doc, save as pops up and will not save... samanthab Word 3 01-19-2013 06:27 AM
Save as PDF with bookmarks Macro VBA "Save as" with bookmarks in file name string Dom37 Word VBA 2 10-31-2011 03:28 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:09 AM.


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