Consulting

Results 1 to 3 of 3

Thread: Saving Doc as PDF and Merging other pdfs in saved pdf

  1. #1
    VBAX Regular
    Joined
    Jul 2019
    Posts
    10
    Location

    Saving Doc as PDF and Merging other pdfs in saved pdf

    Hey everyone,

    I've got a userform that has a command button that saves the active document as a word file and exports as a pdf to a nominated folder location (thanks to the two G wizards!).

    I'm not sure if the following is possible or not and I honestly have no idea how to determine or test if I can get this to work.

    The code asks the user for the folder and then saves the file as said.

    What I want to attempt to do is:

    1. Save the file as word and pdf (this works YAY)
    2. If the folder contains a certain list of files (1.pdf, a54.pdf for example), I want to automatically merge these existing "template files" into the saved pdf.
    3. Open the merged pdf to view

    The created document will always be the exact same amount of pages but two of the "template" files have varying page lengths.

    The code I have for saving is:

    'From my favourite G man!
    Private Sub CommandButton3_Click()
    'Opens a box to nominate folder
    Dim fd As FileDialog
     Dim strFolder As String
     Dim strName As String
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
     With fd
         .Title = "Save The Report To The Job Folder"
         If .Show = -1 Then
             strFolder = .SelectedItems(1)
         Else
             MsgBox "You did not select a folder."
             Exit Sub
         End If
     End With
     
     ChDir strFolder 'stores folder path
     strName = "\GEA." & ActiveDocument.Variables("PN").Value & ".R." & ActiveDocument.Variables("LT") & ".Rev." & ActiveDocument.Variables("RN")
     ActiveDocument.SaveAs2 strFolder & strName & ".docx"
     
     strName = strName & ".pdf"
     ActiveDocument.ExportAsFixedFormat OutputFileName:=strFolder & "\" & strName, _
                                   ExportFormat:=wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
                                   wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, to:=99, _
                                   Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
                                   CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
                                   BitmapMissingFonts:=True, UseISO19005_1:=True
    
    If it's possible --> I think here I need to do something like If Template File A.pdf exists in ChDir then
    " merge with exported document at end of pdf range
                  repeat for 2 or 3 other template files
    
    
    
    End Sub
    Any guidance or help would be amazing - I know I'm asking for a lot here so even if you just point me in the right direction, I would be very grateful.

  2. #2
    VBAX Regular
    Joined
    Jul 2019
    Posts
    10
    Location
    I've tried a few solutions to this now and still no avail!

    I feel the only way to accomplish this is through several fairly large and complex if statements within the saving process but this is well outside my scope of understanding.

    Thank you to everyone who has had a look at the post to consider any assistance.

  3. #3
    Word's PDF function is rudimentary and does not provide for appending documents to existing PDFs. You will need third party PDF software to do that.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •