Link to home
Start Free TrialLog in
Avatar of ClaudeWalker
ClaudeWalker

asked on

Exit process in VBA by using PID

I have a program that activates an application using the PID (app activate) .  I'm wondering if there is a function where I can kill a task using the PID.  

TASKKILL "Excel.exe" is giving me a remote server not found error on the second time I open the document.

Thanks,
JOe K.
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image


  You can use TerminateProcess():

http://msdn.microsoft.com/en-us/library/ms686714(v=vs.85).aspx

  But that's pretty harsh.  It would be better to shutdown the app gracefully.

JimD.
Avatar of ClaudeWalker
ClaudeWalker

ASKER

How could I shut down the app gracefully?
ASKER CERTIFIED SOLUTION
Avatar of danishani
danishani
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Neither wbk.application.quit nor xlApp.quit is getting rid of the process.

So then when I run invExport I either get a <connection to remote server is gone> if I use TaskKill or I get the Instance of Excel message box because the Excel process is still open.

Any ideas on how to get rid of the process completely and have Access not look for that connection anymore?

Thanks,
JOe K.
Sub invImport()
    Dim wbk As Excel.Workbook
    Dim wks As Excel.Worksheet
    Dim xlApp As Excel.Application
    
    If checkExcelProcess("Excel.exe") <> 0 Then
        MsgBox "Please close all instances of excel before running the import utility", vbInformation, "Close Excel"
        Exit Sub
        GoTo err
    End If
    
    Forms("frmInventoryAutomation").Controls("lblWarning").Visible = True
    
    Set wbk = openInvDoc(xlApp)
    Set wks = getSheet(wbk)
    
    If wks Is Nothing Then GoTo err
     
        
    If checkRangeConsistency(wks) <> "" Then
        GoTo err
    End If
    
    Call copyEndSheet(wbk, wks)
    
    AppActivate (checkExcelProcess("MSACCESS.exe"))
    
    Set wks = getSheet(wbk)
    
    If Not checkIfPOsExist(wks) Then
        MsgBox "There are no unprocessed POs", vbInformation, "No unprocessed PO's"
        GoTo err
    End If
        
    If checkBuildingNumbers(wks) Then
        GoTo err
    End If
       
    Call clearTempTables
    Call iterateThroughRows(wks)
    wbk.Save
    xlApp.Quit
    Forms("frmInventoryAutomation").Controls("lblWarning").Visible = False
        
    Exit Sub
err:
    
    wbk.Save
    Forms("frmInventoryAutomation").Controls("lblWarning").Visible = False
    xlApp.Quit
    
    Exit Sub
End Sub

Sub invExport()
    Dim xlApp As Excel.Application
    
    Dim wbk As Excel.Workbook
    Dim wks As Excel.Worksheet
    
    If checkExcelProcess("Excel.exe") <> 0 Then
        MsgBox "Please close all instances of excel before running the import utility", vbInformation, "Close Excel"
        Stop
    End If
    
    Forms("frmInventoryAutomation").Controls("lblWarning").Visible = True
    
    Set wbk = openInvDoc(xlApp)
    Set wks = getSheet(wbk)
    
    If wks Is Nothing Then GoTo err
    
    If checkRangeConsistency(wks) <> "" Then
        GoTo err
    End If
    
    Call copyEndSheet(wbk, wks)
    
    AppActivate (checkExcelProcess("MSACCESS.exe"))
    
    Set wks = getSheet(wbk)
    
    If checkIfPOsExist(wks) Then
        MsgBox "There are no unprocessed POs", vbInformation, "No unprocessed PO's"
        GoTo err
    End If
    
    If checkBuildingNumbers(wks) Then
        GoTo err
    End If
    
    If Not fillOrCheckInvReport(wks, False) Then
        MsgBox "There are totals in the DB and on the report that don't match up.  Check to make sure that the job numbers and totals match up on the Inventory report and the Database", vbInformation, "Descrepency between DB and Report"
        GoTo err
    Else
        Call fillOrCheckInvReport(wks, True)
    End If
    
    Exit Sub
err:
    
    xlApp.Quit
    
    Forms("frmInventoryAutomation").Controls("lblWarning").Visible = False
    Exit Sub
End Sub

Open in new window

Even this is not working.

wbk.Close True
Set wbk = Nothing
   
xlApp.Quit
Set xlApp = Nothing
By clearing the objects I was able to use TASKILL and not get the remote server error.  Works good enough for me!

Thanks,
JOe K.
Awarding points for effort.  Thanks.