excelvbashell

VBA script to open all pdfs in folder in a single instance of Acrobat


Workflow requires user, whilst using a data source in excel, to select a unique ID and then click a button which will open all pdfs in a target folder. I have automated location of the target folder (there are 10,000 subfolders) using the unique ID and an associated date. Am wanting to open all files from target directory in single instance of Acrobat.

The following can loop through the target directory and open all pdfs without any errors. But does so in distinct Acrobat instances. i.e. If there are 5 pdfs, 5 instances of Acrobat are opened. Can anyone please offer advice on a method that would open all the pdfs from the directory in the same instance of acrobat. thx in advance.

Sub OpenPDF1()

Dim path_to_exe As String
Dim floder_path As String
Dim pdf_to_open As String

pdf_to_open = ""

path_to_exe = "C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\AcroRd32.exe"

folder_path = "C:\\test1"

pdf_to_open = Dir(folder_path & "\*.pdf")

    Do While Len(pdf_to_open) > 0
        Shell path_to_exe & " " & folder_path & "\" & pdf_to_open
        pdf_to_open = Dir()
    Loop

End Sub

Solution

  • To open all PDFs in a single instance of Acrobat, you can use the ShellExecute function from the Windows API:

    Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
        ByVal hwnd As LongPtr, _
        ByVal lpOperation As String, _
        ByVal lpFile As String, _
        ByVal lpParameters As String, _
        ByVal lpDirectory As String, _
        ByVal nShowCmd As Long) As Long
    
    Sub OpenPDF1()
        Dim folder_path As String
        Dim pdf_to_open As String
    
        folder_path = "C:\test1\"
        pdf_to_open = Dir(folder_path & "*.pdf")
    
        Do While Len(pdf_to_open) > 0
            ShellExecute 0, "open", folder_path & pdf_to_open, vbNullString, vbNullString, 1
            pdf_to_open = Dir()
        Loop
    End Sub