+ Reply to Thread
Results 1 to 10 of 10

Print to PDF, Merge PDF

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Print to PDF, Merge PDF

    Hi guys I have a macro printing to pdf and then merging with another PDF. How can I save my DestFile to a different location than the folder where I grabbed the 2 PDF's to merge?

    Sub PrintPages()
    
    ThisWorkbook.Sheets(Array("Front Page", "1", "2", "Last Page")).Select
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "s:\Folder\Path\PDF2.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=False
    
        Const MyPath = "S:\Path\Folder\"
        Const MyFiles = "PDF1.pdf,PDF2.pdf"
        Const DestFile = "PDF1and2.pdf"
    
        Dim a As Variant, i As Long, n As Long, ni As Long, p As String
        Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc
        
        If Right(MyPath, 1) = "\" Then p = MyPath Else p = MyPath & "\"
        a = Split(MyFiles, ",")
        ReDim PartDocs(0 To UBound(a))
         
        On Error GoTo exit_
        If Len(Dir(p & DestFile)) Then Kill p & DestFile
        For i = 0 To UBound(a)
    
            If Dir(p & Trim(a(i))) = "" Then
                MsgBox "File not found" & vbLf & p & a(i), vbExclamation, "Canceled"
                Exit For
            End If
    
            Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
            PartDocs(i).Open p & Trim(a(i))
            If i Then
    
                ni = PartDocs(i).GetNumPages()
                If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 0, ni, True) Then
                    MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled"
                End If
    
                n = n + ni
    
                PartDocs(i).Close
                Set PartDocs(i) = Nothing
            Else
    
                n = PartDocs(0).GetNumPages()
            End If
        Next
         
        If i > UBound(a) Then
    
            If Not PartDocs(0).Save(PDSaveFull, p & DestFile) Then
                MsgBox "Cannot save the resulting document" & vbLf & p & DestFile, vbExclamation, "Canceled"
            End If
        End If
         
    exit_:
         
        If Err Then
            MsgBox Err.Description, vbCritical, "Error #" & Err.Number
        ElseIf i > UBound(a) Then
            MsgBox "The resulting file is created:" & vbLf & p & DestFile, vbInformation, "Done"
        End If
    
        If Not PartDocs(0) Is Nothing Then PartDocs(0).Close
        Set PartDocs(0) = Nothing
         
        AcroApp.Exit
        Set AcroApp = Nothing
    
    End Sub

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,703

    Re: Print to PDF, Merge PDF

    Instead of the following line of code, set p to whatever location you want to save it to
    If Right(MyPath, 1) = "\" Then p = MyPath Else p = MyPath & "\"
    For example
    Const p As String = "S:\Path\Other Folder\"
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Re: Print to PDF, Merge PDF

    Thx Jazz, I keep getting a Duplicate declaration in current scope comp error though?

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,703

    Re: Print to PDF, Merge PDF

    Oh, that's because p is already declared. So you can use
    p = "S:\Path\Other Folder\"
    There are a few odd techniques in this code. For example, MyPath is a Const but there is an If statement checking to see if it ends in a "\".

  5. #5
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Re: Print to PDF, Merge PDF

    What do you suggest to clean up the code? I am just looking to combine 2 pdf's in one folder and save the combined PDF in another folder

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,703

    Re: Print to PDF, Merge PDF

    It's not wrong, it's just a little unusual and I'm not sure why it was done. I assume you did not write all this yourself. Try the change I suggested and see if that gets you what you need.

  7. #7
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Re: Print to PDF, Merge PDF

    Thanks again Jazz, still not working tho, i'm sure something is wrong with my two folder locations and MyPath:
    Sub PrintPages()
    ThisWorkbook.Sheets(Array("Front Page", "Last Page")).Select
    
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "s:\Location1\PDF1.pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=False
    
        Const MyPath = "S:\Location 1\Folder\"
        Const MyFiles = "PDF1.pdf,PDF2.pdf"
        Const DestFile = "MERGED PDF.pdf"
    
        Dim a As Variant, i As Long, n As Long, ni As Long, p As String
        Dim AcroApp As New Acrobat.AcroApp, PartDocs() As Acrobat.CAcroPDDoc
        
        If Right(p, 1) = "\" Then p = "S:\Location 2\Folder\"
        a = Split(MyFiles, ",")
        ReDim PartDocs(0 To UBound(a))
         
        On Error GoTo exit_
        If Len(Dir(p & DestFile)) Then Kill p & DestFile
        For i = 0 To UBound(a)
    
            If Dir(p & Trim(a(i))) = "" Then
                MsgBox "File not found" & vbLf & p & a(i), vbExclamation, "Canceled"
                Exit For
            End If
    
            Set PartDocs(i) = CreateObject("AcroExch.PDDoc")
            PartDocs(i).Open p & Trim(a(i))
            If i Then
    
                ni = PartDocs(i).GetNumPages()
                If Not PartDocs(0).InsertPages(n - 1, PartDocs(i), 0, ni, True) Then
                    MsgBox "Cannot insert pages of" & vbLf & p & a(i), vbExclamation, "Canceled"
                End If
    
                n = n + ni
    
                PartDocs(i).Close
                Set PartDocs(i) = Nothing
            Else
    
                n = PartDocs(0).GetNumPages()
            End If
        Next
         
        If i > UBound(a) Then
    
            If Not PartDocs(0).Save(PDSaveFull, p & DestFile) Then
                MsgBox "Cannot save the resulting document" & vbLf & p & DestFile, vbExclamation, "Canceled"
            End If
        End If
         
    exit_:
    
        If Err Then
            MsgBox Err.Description, vbCritical, "Error #" & Err.Number
        ElseIf i > UBound(a) Then
            MsgBox "The resulting file is created:" & vbLf & p & DestFile, vbInformation, "Done"
        End If
    
        If Not PartDocs(0) Is Nothing Then PartDocs(0).Close
        Set PartDocs(0) = Nothing
         
        AcroApp.Exit
        Set AcroApp = Nothing
    End Sub

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,703

    Re: Print to PDF, Merge PDF

    Please explain what "not working" means.
    • Does the code run?
    • Does it run but do nothing?
    • Does it produce error messages? If so, what do the messages say?
    • Does it stop in the debugger? On what line of code?
    • Does it produce unexpected/wrong results?
    • Does it hang?

  9. #9
    Forum Contributor
    Join Date
    08-23-2013
    Location
    Alberta, Canada
    MS-Off Ver
    Excel 365
    Posts
    166

    Re: Print to PDF, Merge PDF

    The PDF print is working to the right location but I get a "File not found" for PDF2.pdf here:

    Const MyFiles = "PDF1.pdf,PDF2.pdf"

  10. #10
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2411
    Posts
    26,703

    Re: Print to PDF, Merge PDF

    You can't possibly get a File Not Found on that line of code. It's just a declaration with a string assignment. What line is highlighted in yellow when the error occurs?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] vba to print mail merge results
    By excelloser in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-01-2012, 10:49 AM
  2. Replies: 2
    Last Post: 07-12-2012, 08:11 PM
  3. print merge
    By Pavczech in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-16-2011, 06:11 PM
  4. Print mail merge document
    By LisaPatch in forum Excel Programming / VBA / Macros
    Replies: 31
    Last Post: 10-31-2011, 09:54 AM
  5. Print merge from within excel
    By excel passion in forum Excel General
    Replies: 2
    Last Post: 05-26-2010, 12:43 AM
  6. [SOLVED] Print merge file
    By ShadowVixen in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-11-2006, 04:15 PM
  7. [SOLVED] Envelopes will not print using mail merge.
    By mission in forum Excel General
    Replies: 1
    Last Post: 09-29-2005, 06:05 AM
  8. [SOLVED] Merge and print
    By Einar in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-16-2005, 06:05 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1