title: Printing a PDF file and terminating Adobe Reader
body: <p>I'm trying to export a sheet <code>Sheet1</code> as a PDF file and print it then close adobe reader using a code.</p>
  
  <p>This is the code I'm using:</p>
  
  <pre><code>Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" 
      (ByVal hwnd As  Long, 
      ByVal lpOperation As String, 
      ByVal lpFile As String, 
      ByVal lpParameters As String, 
      ByVal lpDirectory As String, 
      ByVal nShowCmd As Long) As Long
  
  Public Function PrintThisDoc(Formname As Long, FileName As String)
    On Error Resume Next
    Dim X As Long
    X = ShellExecute(Formname, "Print", FileName, 0&amp;, 0&amp;, 1)
  End Function
  
  Private Sub CommandButton1_Click()
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:="D:\Print.pdf"
    PrintThis = PrintThisDoc(0, "D:\Print.pdf")
    Application.Wait (Now + TimeSerial(0, 0, 10))
    TerminateProcess
  End Sub
  </code></pre>
  
  <p>I use the <code>.wait</code> method to pause the code for 10 seconds until the print order is sent to the printer before terminating adobe reader.
  But when I add this line, the <code>TerminateProcess</code> sub is affected and I get a <code>Not found error</code> concerning this line (<code>intError = objProcess.Terminate</code>)</p>
  
  <pre><code>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:" &amp; "{impersonationLevel=impersonate}!\\.\root\cimv2")
  
    'Find the process to terminate
    Set objList = objWMIcimv2.ExecQuery("select * from win32_process where name='" &amp; strTerminateThis &amp; "'")
  
    'Terminates a process and all of its threads.
    For Each objProcess In objList
        intError = objProcess.Terminate
    Next
  
    Set objWMIcimv2 = Nothing
    Set objList = Nothing
    Set objProcess = Nothing
  End Sub
  </code></pre>
  
  <p>when I remove the <code>.wait</code> line, Adobe reader is terminated but nothing is printed as my file is opened and closed in no time.</p>
  
  <p>So any help with that please ??</p>