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

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.
0
ClaudeWalker
Asked:
ClaudeWalker
  • 5
  • 2
2 Solutions
 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:

  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.
0
 
ClaudeWalkerAuthor Commented:
How could I shut down the app gracefully?
0
 
danishaniCommented:
In VBA something likg Application.Quit when in Excel.

HTH,
Daniel
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Jim Dettman (Microsoft MVP/ EE MVE)President / OwnerCommented:
<<How could I shut down the app gracefully?>>

  As Daniel has said, for the Office apps, it's Application.Quit.

  What is it exactly that your doing and what's the issue your running into?

JimD.
0
 
ClaudeWalkerAuthor Commented:
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

0
 
ClaudeWalkerAuthor Commented:
Even this is not working.

wbk.Close True
Set wbk = Nothing
   
xlApp.Quit
Set xlApp = Nothing
0
 
ClaudeWalkerAuthor Commented:
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.
0
 
ClaudeWalkerAuthor Commented:
Awarding points for effort.  Thanks.
0
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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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