?
Solved

Exit process in VBA by using PID

Posted on 2011-05-02
8
Medium Priority
?
1,864 Views
Last Modified: 2012-06-21
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
Comment
Question by:ClaudeWalker
  • 5
  • 2
8 Comments
 
LVL 58
ID: 35508829

  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
 

Author Comment

by:ClaudeWalker
ID: 35508891
How could I shut down the app gracefully?
0
 
LVL 12

Accepted Solution

by:
danishani earned 1000 total points
ID: 35509035
In VBA something likg Application.Quit when in Excel.

HTH,
Daniel
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 58

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 1000 total points
ID: 35512254
<<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
 

Author Comment

by:ClaudeWalker
ID: 35513302
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
 

Author Comment

by:ClaudeWalker
ID: 35513712
Even this is not working.

wbk.Close True
Set wbk = Nothing
   
xlApp.Quit
Set xlApp = Nothing
0
 

Author Comment

by:ClaudeWalker
ID: 35514121
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
 

Author Closing Comment

by:ClaudeWalker
ID: 35514130
Awarding points for effort.  Thanks.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question