• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 493
  • Last Modified:

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

  • 3
  • 2
1 Solution
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: http://support.adobe.com/devsup/devsup.nsf/docs/52080.htm

According to the documentation you can use the /t switch.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now