Printing separate reports into one combined PDF using VBA? (1 Viewer)

LDW

Registered User.
Local time
Yesterday, 19:40
Joined
Dec 31, 2012
Messages
19
Currently I have five separate reports setup in Access 2010 (that are running off of five separate queries). Since they have different data/fields, I was unable to combine into one query & one report.

However, I print each to PDF and then combine using Acrobat Pro. Is there a way to combine in VBA and not have to do the step using Acrobat Pro?
Function PrintReports()

DoCmd.OpenReport "Program_Summary_1", acViewPreview
DoCmd.OutputTo acOutputReport, "Program_Summary_1", "PDFFormat(*.pdf)", "C:\Desktop\PrintFiles\" & "Program_Summary_1" & ".pdf", False
DoCmd.Close acReport, "Program_Summary_1"

DoCmd.OpenReport "Program_Summary_2, acViewPreview
DoCmd.OutputTo acOutputReport, "Program_Summary_2", "PDFFormat(*.pdf)", "C:\Desktop\PrintFiles\" & "Program_Summary_2" & ".pdf", False
DoCmd.Close acReport, "Program_Summary_2"

DoCmd.OpenReport "Program_Summary_3", acViewPreview
DoCmd.OutputTo acOutputReport, "Program_Summary_3", "PDFFormat(*.pdf)", "C:\Desktop\PrintFiles\" & "Program_Summary_3" & ".pdf", False
DoCmd.Close acReport, "Program_Summary_3"

DoCmd.OpenReport "Program_Summary_4", acViewPreview
DoCmd.OutputTo acOutputReport, "Program_Summary_4", "PDFFormat(*.pdf)", "C:\Desktop\PrintFiles\" & "Program_Summary_4" & ".pdf", False
DoCmd.Close acReport, "Program_Summary_4"

DoCmd.OpenReport "Program_Summary_5", acViewPreview
DoCmd.OutputTo acOutputReport, "Program_Summary_5", "PDFFormat(*.pdf)", "C:\Desktop\PrintFiles\" & "Program_Summary_5" & ".pdf", False
DoCmd.Close acReport, "Program_Summary_5"

MsgBox ("Reports hav been printed.")

End Function
Thanks for the help!
 

sxschech

Registered User.
Local time
Yesterday, 16:40
Joined
Mar 2, 2010
Messages
792
Here is some code to combine pdf files. I tried to modify based on your description. Can't promise it will run as is though. If loop doesn't work, may need to try hard coding the report names if you only have a few. My actual code loop starts at 0, but your report name started at one so I have tried replacing the zero with one. hopefully it will work for you.

Code:
Sub MergePDF()





    'Combined multiple PDF files into one
    'set a reference to Acrobat (Adobe Acrobat 7.0 Type Library)
    'http://www.khk.net/wordpress/2009/03/04/adobe-acrobat-and-vba-an-introduction/
    '30-JUL-2010
    
    'Added code to close the open pdf files (except for the final merged report)
    '09-Jun-2011
    
    'May need to see if file exists when this is run more than once.  Will add if needed
    
    Dim AcroApp As Acrobat.CAcroApp
    
    Dim Part1Document As Acrobat.CAcroPDDoc
    Dim Part2Document As Acrobat.CAcroPDDoc
    
    Dim numPages As Integer
    Dim pdfsrc As String
    Dim x As Integer
    Dim stMergename As String
    
    Set AcroApp = CreateObject("AcroExch.App")
    
    Set Part1Document = CreateObject("AcroExch.PDDoc")
    Set Part2Document = CreateObject("AcroExch.PDDoc")
         
    pdfsrc = "C:\Desktop\PrintFiles\Program_Summary_1.pdf"
     
    x = 2
    
    Part1Document.Open (pdfsrc)
    Part2Document.Open (Replace(pdfsrc, "1", x))
        
    Do While x < 6
        ' Insert the pages of Part2 after the end of Part1
        numPages = Part1Document.GetNumPages()
        
        If Part1Document.InsertPages(numPages - 1, Part2Document, 0, Part2Document.GetNumPages(), True) = False Then
            MsgBox "Cannot insert pages for " & (Replace(pdfsrc, "1", x)) & ".  See if it is on the disk.  If not, please recreate."
            MsgBox "Close all open Adobe Acrobat windows, before reprinting these reports."
            Exit Sub
        End If
         
        x = x + 1
        Part2Document.Close
        
        'Close the open pdf files except for the Merged report
        'FollowHyperlink Replace(pdfsrc, "1", X - 1), , True, False
        'SendKeys "%{F4}", False
        DoEvents
        On Error Resume Next
        Call WinClose(Replace(Mid(pdfsrc, InStrRev(pdfsrc, "\") + 1), "1", x - 1) & " - Adobe Acrobat Pro")
        DoEvents
        
                
        Part2Document.Open (Replace(pdfsrc, "1", x))
        'Debug.Print (Replace(pdfsrc, "1", x))
    Loop
    

    stMergename = "C:\Desktop\PrintFiles\Program_Summary_Combined.pdf"
    If Part1Document.Save(PDSaveFull, stMergename) = False Then
        MsgBox "Cannot save the modified document"
    End If
        
    Part1Document.Close
    Part2Document.Close
     
    AcroApp.Exit
    Set AcroApp = Nothing
    Set Part1Document = Nothing
    Set Part2Document = Nothing
    
    'Open merged file for review and printing
    FollowHyperlink stMergename, , True, False
    MsgBox "Done"
    
End Sub
 

sxschech

Registered User.
Local time
Yesterday, 16:40
Joined
Mar 2, 2010
Messages
792
If you use the winclose instead of sendkeys, need to put this in its own module.

Code:
Option Compare Database

'http://www.andreavb.com/forum/viewtopic_5604.html
'Post:Asim-GDI GURU

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Const SW_SHOWNORMAL = 1
Const WM_CLOSE = &H10

Public Sub WinClose(WindowToClose As String)
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim WinWnd As Long ', Ret As String,
    Dim RetVal As Long, lpClassName As String
    'Ask for a Window title
    'Ret = InputBox("Enter the exact window title:" + Chr$(13) + Chr$(10) + "Note: must be an exact match")
    ret = WindowToClose
    'Search the window
    WinWnd = FindWindow(vbNullString, ret)
    If WinWnd = 0 Then MsgBox "Couldn't find the window ...": Exit Sub
    'Show the window
    ShowWindow WinWnd, SW_SHOWNORMAL
    'Post a message to the window to close itself
    PostMessage WinWnd, WM_CLOSE, 0&, 0&
End Sub
 

LDW

Registered User.
Local time
Yesterday, 19:40
Joined
Dec 31, 2012
Messages
19
Wow, that was really beyond my vba knowledge. Might take me a while to work through, but thank you so much for the reply!
 

levimauricio

New member
Local time
Yesterday, 17:40
Joined
Sep 12, 2023
Messages
2
13 years later, the solution is still being implemented, thank you very much for sharing the code
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:40
Joined
Oct 29, 2018
Messages
21,453
13 years later, the solution is still being implemented, thank you very much for sharing the code
Hi. Welcome to AWF!

I see two codes suggested, which one did you implement?
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 16:40
Joined
Oct 29, 2018
Messages
21,453
I used both, in the form I placed the Sub Merge PDF and in a module I placed the Public Sub WinClose
Okay, thanks for the clarification.

In case you could use it as well, take a look at this old post.
 

Users who are viewing this thread

Top Bottom