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?




    2012年3月23日 13:00



    • 已标记为答案 RLN_SQL 2012年3月27日 21:52
    2012年3月23日 13:28
  • Thank you!

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


    2012年3月23日 13:39
  • Got it to working!  Thank you for your help here!


    2012年3月27日 21:52
  • 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>


    2012年4月12日 16:19
  • 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

    • 已建议为答案 Andreg52 2014年3月30日 8:44
    2012年4月12日 16:30
  • 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?) 


    2012年4月19日 18:33
  • For the reader it's AcroviewR10

    for the app it's AcroviewA10

    • 已编辑 Andreg52 2014年3月30日 9:08
    • 已标记为答案 RLN_SQL 2014年8月5日 13:59
    2014年3月30日 8:44
  • 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

    2014年5月7日 10:51