Full Version: How do I end a process from access
UtterAccess Discussion Forums > Microsoft® Access > Access Automation
maxamis4
Hello folks,

Question for any of you gurus out there. how do i end a windows process such as excel through microsoft Access. I know its vba but where do i find examples on how to do this.

Thanks.
dannyseager
Create a batch file and add this in it...

taskkill /F /IM Excel.exe

Then call that from access.

for some more info check out : http://www.microsoft.com/resources/documen...l.mspx?mfr=true
maxamis4
Check this one out

http://www.vbforums.com/showthread.php?t=364654
r_cubed
You can close ANY (and AQLL) processes via VBA if you know the 'ClassName' of the application/s that you want to close.

The code below will do it for you, and it already includes a example of the required clasname for Excel (along with a number of the more popular applications as well).

Simply make a call to the function "Close_Running_Application' passing he appropriate 'classname' as a parameter to it.

P.S. IF ( ? ) you want the code that I refer to which can allow you to determine the 'classname' of ANY application (and therefore then make it avaialable for use with the 'Close_Running_Application'), post back and I will post THAT code as well.

CODE
Option Compare Database
Option Explicit

Private Const WM_CLOSE = &H10
Private Const INFINITE = &HFFFFFFFF

Private Declare Function apiPostMessage _
    Lib "user32" Alias "PostMessageA" _
    (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) _
    As Long

Private Declare Function apiFindWindow _
    Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassname As String, _
    ByVal lpWindowName As String) _
    As Long
    
Private Declare Function apiWaitForSingleObject _
    Lib "kernel32" Alias "WaitForSingleObject" _
    (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) _
    As Long
    
Private Declare Function apiIsWindow _
    Lib "user32" Alias "IsWindow" _
    (ByVal hwnd As Long) _
    As Long
        
Private Declare Function apiGetWindowThreadProcessId _
    Lib "user32" Alias "GetWindowThreadProcessId" _
    (ByVal hwnd As Long, _
    lpdwProcessID As Long) _
    As Long
        
Function Close_Running_Application(lpClassname As String, _
                   Optional lpWindowName As String = vbNullString) _
             As Boolean
             '
' Can close any application that is running. To do it you have to KNOW what the actual ClassName
' of the actual application is.
' This can be determined by running the function "Show_Running_Application" from the Immediate Window
' and then passing that to THIS function
'
'Usage Examples:
'   To close an application:
'       ? Close_Running_Application("SciCalc")          ' Calculator
'       ? Close_Running_Application("XLMAIN")           ' ALL versions of Excel, any spreadsheet
'       ? Close_Running_Application("rctrl_renwnd32"    ' Outlook XP
'       ? Close_Running_Application("ExploreWClass")    ' Windows Explorer
'       ? Close_Running_Application("IEFrame")          ' Internet Explorer
'       ? Close_Running_Application("EXCLICKYES_WND")   ' ClickYes
'       ? Close_Running_Application("ShockwaveFlash")   ' Flash
'
' ------------------------------------------------------------------------------------
' Media Players:
'       ? Close_Running_Application("WMPlayerApp")                      ' Windows Media Player
'       ? Close_Running_Application("Media Player 2")                   ' Windows Media Player 2
'       ? Close_Running_Application("MediaPlayerClassicW")              ' Windows Media Player CLASSIC
'       ? Close_Running_Application("STUDIO")                           ' WinAMP3
'       ? Close_Running_Application("QWidget")                          ' DivX Player
'       ? Close_Running_Application("WinDVDClass")                      ' WinFastDVD
'       ? Close_Running_Application("MMJB:MAINWND")                     ' MUSICMATCH JukeBox
'       ? Close_Running_Application("RadioToolBox v1.x")                ' Radio Toolbox
'       ? Close_Running_Application("CyberLink Video Window Class")     ' PowerDVD
'
' ------------------------------------------------------------------------------------

'   To close a specific file running under an application:
'       ? Close_Running_Application("XLMAIN","Microsoft Excel - MySheet.xls")  ' ALL versions of Excel,specific spreadsheet

    Dim lngRet As Long, hwnd As Long, pID As Long
    
    Close_Running_Application = False

    hwnd = apiFindWindow(lpClassname, lpWindowName)
    If (hwnd) Then
        lngRet = apiPostMessage(hwnd, WM_CLOSE, 0, ByVal 0&)
        Call apiGetWindowThreadProcessId(hwnd, pID)
        Call apiWaitForSingleObject(pID, INFINITE)
        Close_Running_Application = Not (apiIsWindow(hwnd) = 0)
        Close_Running_Application = True
    End If
    
End Function
'************* Code End ***************

Function Close_ALL_Occurrences_Of_Application(lpClassname As String) As Boolean

    Dim bolApplnClosed As Boolean
    Dim MaxCloseCount As Integer
        
    Do Until MaxCloseCount = 25
        MaxCloseCount = MaxCloseCount + 1
        bolApplnClosed = Close_Running_Application(lpClassname)
    Loop

End Function
maxamis4
OKay thanks for the code. I just need some help understanding they way the code works. Where is the class lpClassname coming from? Its being passed through to different functions. The second question is lpClassname = Close_Running_Application("XLMAIN") correct.

thanks
r_cubed
I had examples of how to run the function in the original post.

To close Excel from anywhere via VBA, you simply ONLY have to do the following:

Close_Running_Application("XLMAIN")

The lClassName that you were asking about is simply the parameter that is being passed to the function ( i.e. the "XLMAIN" part.

As I said before, I have another function that I run (as a developer) to determine what the actual 'ClassName' is for any/all applications. This has to be done off-line, and normally outside of any actual application.

The way it has to be done is to (say) open Excel up , then go to an Access Immediate Window, and run this other function. It will then show the 'classname' for any/all currently running applications (i.e. in this case, Excel), and then THAT classname can be used by the 'Close_Running_Application' function.

Normally, when I want to get a classname of an application, I would open up any number of diferent apps and then run my function. It shows EVERY currently open applications classname. It does NOT have to be one-at--time.

Again, if you want this other bit of code, ask and I will find it and post it ....
maxamis4
Hey thanks a bunch that worked out. I appreciate it.
r_cubed
No problem, glad it worked out for yuou.

Just as an additional bit of info, ..... the Close_Running_Application can als o e used to close a SPECIFIC instance of an application (i.e. a specific Excel speadsheet), if you pass its name to the function as the optional 2nd parameter.
maxamis4
So I ran into a problem, the process would not always end. I am not sure if something in my code keeps it running but I had to find something that will kill the process completely like the batch script did. here it is thanks to the both of you
This actually runs from within VB/Access so you don't have to generate any additional file

CODE
Function my_Excel_close()
Dim command As String
command = "taskkill /F /IM Excel.exe"
Dim wsShell, Proc
   Set wsShell = CreateObject("wscript.shell")
   Set Proc = wsShell.Exec(command)
Do While Proc.Status = 0
      DoEvents 'Yields execution so that the operating system can process other events
   Loop

End Function
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.