Closing PDF files that are open via VBA


  • RE: Access 2003

    In my application (via command button) I have successfully launched Adobe Reader with a PDF file. 

    The user can have multiple pdf's open (up to 4).

    Does anyone have a VBA code example that will   1) check to see if Adobe Reader is active in the Task Manager, and then 2) close file "12-3456.pdf", but leave other .pdf files open for the user to view?




    Friday, March 23, 2012 1:00 PM


All replies

    • Marked as answer by RLN_SQL Tuesday, March 27, 2012 9:52 PM
    Friday, March 23, 2012 1:28 PM
  • Thank you!

    (I was actually thinking it would be a little simpler, but I guess not.)


    Friday, March 23, 2012 1:39 PM
  • Got it to working!  Thank you for your help here!


    Tuesday, March 27, 2012 9:52 PM
  • Using the code from these links:

    ......initially worked for closing one window, but now the requirement has changed.  The user wants to close multiple .pdf windows.

    (RE: Access 2003/  Adobe Reader X ver. 10.1.2)

    When I process contracts in my app, I display .pdf files related to that contract for the user while they process the current contract.

    When I go from contract #1 to contract #2, contract #1's .pdf file windows need to be closed.
    I have no problem opening Adobe Reader and displaying the pdf file, that works very nicely. 

    The problem is in closing multiple open .pdf file windows.  Up to two .pdf files can be opened and displayed for any given contract being processed

    Per the Debug window example here, I need to modify my code to where I can close *all* the open .pdf Adobe Reader sessions for the previous contract
    where the class = "AcrobatSDIWindow" and
    Caption = "<strPrevContractNum>.pdf" and "<strPrevContractNum>.-Invoice.pdf" 
    From the Debug window:
    Class = AcrobatSDIWindow    Caption = 5-22894.pdf - Adobe Reader
    Class = AcrobatSDIWindow    Caption = 5-22894-Invoice.pdf - Adobe Reader
    Class = AcrobatSDIWindow    Caption = 5-22893-Invoice.pdf - Adobe Reader

    Per the debug window example, I processed contract 5-22894.pdf first.
    Then I launched contract 5-22893.  At the time I launched file "893", I need to close both of the other .pdf files from the previous contract, 5-22894.

    Currently, the code listed below will only close the most recently opened .pdf file.

    Any suggestions would be helpful...thanks.

    These functions I am using:  fEnumWindows,  fIsAppRunning and   fCloseApp  all came from the links above.

    Here is the code I am using currently:

    <begin code>

    Public Sub ClosePDF()

    'interrogates to see if Adobe Reader is running in the taskbar and if it is,
    'this routine will close the Adobe Reader session that is currently running...

    On Error GoTo Err1
        'Close the .pdf file for this contract if it is currently opened...
        fEnumWindows   'get the class names for opened windows in task bar
        Dim blnResult As Boolean
        blnResult = fIsAppRunning("AdobeAcrobat")
        If blnResult = True Then
            fCloseApp ("AcrobatSDIWindow")
        End If

        Exit Sub

        MsgBox Err.Number & "--" & Err.Description, vbOKOnly, gblPgmName & "--" & "ClosePDF()"
        Resume Exit1

    End Sub


    <end code>


    Thursday, April 12, 2012 4:19 PM
  • Well I'm using other aproach.

    I'm using dde call to acrobat.

        DDEChan = DDEInitiate("Acroview", "Control")
        DDEExecute DDEChan, "[FilePrintSilent(" & Chr(34) & fullPath & Chr(34) & ")]"
        Pause (60)
        DDEExecute DDEChan, "[AppExit()]"
        DDETerminate DDEChan

    IN acrobat X they chaneged Acroview to acroviewR10

    • Proposed as answer by Andreg52 Sunday, March 30, 2014 8:44 AM
    Thursday, April 12, 2012 4:30 PM
  • I have not used DDE before in my VBA code.  If you have two open .pdf files, how does your DDE code here close *both* of those sessions instead of just the one most recently opened?   How is "DDEChan" and DDEExecute declared?  (what is the syntax?) 


    Thursday, April 19, 2012 6:33 PM
  • For the reader it's AcroviewR10

    for the app it's AcroviewA10

    • Edited by Andreg52 Sunday, March 30, 2014 9:08 AM
    • Marked as answer by RLN_SQL Tuesday, August 05, 2014 1:59 PM
    Sunday, March 30, 2014 8:44 AM
  • This function closes all open PDf files

    Sub TerminateProcess()
        Dim strTerminateThis As String
        Dim objWMIcimv2 As Object
        Dim objProcess As Object
        Dim objList As Object
        Dim intError As Integer
        'Process to terminate
        strTerminateThis = "AcroRd32.exe"
        'Connect to CIMV2 Namespace
        Set objWMIcimv2 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
        'Find the process to terminate
        Set objList = objWMIcimv2.ExecQuery("select * from win32_process where name='" & strTerminateThis & "'")
        'Terminates a process and all of its threads.
        For Each objProcess In objList
            intError = objProcess.Terminate
        Set objWMIcimv2 = Nothing
        Set objList = Nothing
        Set objProcess = Nothing
    End Sub

    Wednesday, May 07, 2014 10:51 AM