shellexecute and closing subsequent window

I am running shell execute to print PDFs, but need to close the PDF screen afterwards. I got some of the code, but am missing the code for CloseHandle -  see Code. The pid is the return value from the shellexecute command. Any ideas what the code for the CloseHandle could be?
Public Function KillProcess(pid As Long) As Long
    Dim hProcess As Long
    hProcess = OpenProcess(PROCESS_TERMINATE, 0, pid)
    If hProcess Then
        KillProcess = TerminateProcess(hProcess, 1&)
        CloseHandle hProcess
    End If
End Function

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Please post your entire code this includes the execution portion. If you are refering to the API ShellExecute Function then this does not return a process id.
However the Shell() function built into VB does return a process id.

Closehandle is being used correctly this has nothing to do with terminating the application but releases the handle returned from openprocess.
jwagmanAuthor Commented:
Is CloseHandle also an API function which I have to declare? I've attached the code - hope it helps. I kill the very last # returned.
 For iIndex = 1 To colFilesPrint.Count
     '   sPDF = sDownloadDir & CStr(colFilesPrint(iIndex))
        sPDF = ""
'        sPDF = sCurrentLocation & sSourceFile & "_" & CStr(colFilesPrint(iIndex))
        sPDF = sCurrentLocation & CStr(colFilesPrint(iIndex))
        If Len(sPDF) > 0 Then
            If FSO.FileExists(sPDF) Then
                lReturnVal = 0
                lReturnVal = ShellExecute(0, "print", sPDF, vbNullString, vbNullString, 0)
                txtfile.WriteLine ("Printing")
                txtfile.WriteLine ("lReturnVal for ShellExecute: " & lReturnVal)
                txtfile.WriteLine ("FileNumber:" & iIndex)
                txtfile.WriteLine ("FileName:" & sPDF)
                If Err.Number <> 0 Then
                    txtfile.WriteLine ("Error: " & Err.Description)
                    txtfile.WriteLine ("No Error")
                End If
            End If
        End If
    Next iIndex
    KillProcess (lReturnVal)

Open in new window

ShellExecute() doesn't return a pid so your call to OpenProcess fails.

<< Is CloseHandle also an API function which I have to declare >>
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

jwagmanAuthor Commented:
Could you recommend an alternative to ShellExecute which accomplishes the same thing but which I can control,i.e. start and stop when I want? TY
A quick search on adobe support leaded me to this here:

According to the documentation you can use the /t switch.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.