Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

A weblog about Microsoft Access, SQL Azure, and SQL Server Tips and Code Samples
Browse by Tags · View All
Access Tips 45
Microsoft Access 45
VBA 41
database 33
Access 33
Code 32
Software development 31
Microsoft Access Software Development 26
Free Access Downloads 25
Software 25

Archive · View All
October 2010 4
September 2008 4
June 2009 4
January 2008 3
July 2009 3
June 2007 2
May 2009 2
March 2011 2
July 2010 2
August 2011 2

Patrick Wood's Blog

Microsoft Access: The Terminator of Processes

Jun 10 2009 7:49AM by Patrick Wood   

Microsoft Access can Terminate Processes using WMI. In other words, Access can shut down an application that is running on a computer. This capability is very useful when using Office automation or automation with other applications. For example, you may want to open an Excel Spreadsheet, import data, and then close Excel using the code in this article.

Access VBA can also be used to terminate a program when it “hangs” or “freezes”. I have at times thorougly enjoyed terminiating some repeat offenders using Access or VBSripts.

The code below shows the basics of how we can terminate a Process. We are using the name of the process in the WQL Query so we do not have to loop through all of the running processes. For example, if we want to close Notepad we can call the procedure using the name of the process as an argument like this:
Call TerminateProcess(“notepad.exe”)

Sub TerminateProcess(strProcess As String)

    Dim objWMIService As Object
    Dim colProcesses As Object
    Dim objProcess As Object
    Dim strComputer As String

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\" _
        & strComputer & "\root\cimv2")

    Set colProcesses = objWMIService.ExecQuery _
        ("SELECT * FROM Win32_Process WHERE" & _
        " Name = '" & strProcess & "'")

    For Each objProcess In colProcesses
        objProcess.Terminate
    Next objProcess

    Set objProcess = Nothing
    Set colProcesses = Nothing
    Set objWMIService = Nothing

End Sub

We can make this code more helpful by getting the result of the Terminate Method. We can change the procedure to a function that will return the result as an integer. We can also use a Message Box. The modified code below does both.

Function ProcessTerminate(strProcess As String) As Long

    Dim objWMIService As Object
    Dim colProcesses As Object
    Dim objProcess As Object
    Dim strComputer As String
    Dim strMsg As String
    Dim intReturn As Long
    Dim booFound As Boolean

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\" _
        & strComputer & "\root\cimv2")

    Set colProcesses = objWMIService.ExecQuery _
        ("SELECT * FROM Win32_Process WHERE" & _
        " Name = '" & strProcess & "'")

    For Each objProcess In colProcesses
        ' The Process is running - set the flag to True
        booFound = True
        ' End the Process and get the Return code
        intReturn = objProcess.Terminate
    Next objProcess

    ' Start a Message string
    strMsg = "   Process Name:  " & strProcess & "      " & vbCrLf & vbCrLf

    ' Determine the result
    Select Case intReturn
        Case 0
            If booFound = True Then
                strMsg = strMsg & "   Termination succeeded.       "
            Else
                strMsg = strMsg & "   Unknown failure.      "
                intReturn = 8
            End If
        Case 2
            strMsg = strMsg & "   Access denied.       "
        Case 3
            strMsg = strMsg & "   Insufficient privilege.       "
        Case 8
            strMsg = strMsg & "   Unknown failure.       "
        Case 9
            strMsg = strMsg & "   Path not found.       "
        Case 21
            strMsg = strMsg & "   Invalid parameter.       "
        Case Else
            strMsg = strMsg & "   Termination failed for unknown reason.       "
    End Select

    strMsg = strMsg & vbCrLf & vbCrLf & "   Time: " & Now
    MsgBox strMsg

    ProcessTerminate = intReturn

    Set objProcess = Nothing
    Set colProcesses = Nothing
    Set objWMIService = Nothing

End Function

We can also Terminate more than one process by creating an array of process names.

Sub ProcessesTerminate()

    Dim objWMIService As Object
    Dim colProcesses As Object
    Dim objProcess As Object
    Dim strComputer As String
    Dim intReturn As Long
    Dim strTargetProc
    Dim arrTargetProcs

    strComputer = "."

	' Create the array of process names
    arrTargetProcs = Array("mspaint.exe", "calc.exe", "notepad.exe")

    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\" _
        & strComputer & "\root\cimv2")

    Set colProcesses = objWMIService.ExecQuery( _
        "SELECT * FROM Win32_Process", , 48)

    Debug.Print "Checking for target processes ..."

    For Each objProcess In colProcesses
        For Each strTargetProc In arrTargetProcs
            ' Make all process names lowercase so they will match
            If LCase(objProcess.Name) = LCase(strTargetProc) Then
                Debug.Print vbCrLf & "Process Name: " & objProcess.Name
                Debug.Print "  Time: " & Now
                intReturn = objProcess.Terminate
                If intReturn = 0 Then
                    Debug.Print "  Terminated"
                Else
                    Debug.Print "  Unable to terminate"
                End If
            End If
        Next
    Next

    Set objProcess = Nothing
    Set colProcesses = Nothing
    Set objWMIService = Nothing

End Sub

We will learn more about how VBA can leverage the capabilities of WMI in the articles to come.


Republished from Access Easy Tips [18 clicks].  Read the original version here [32134 clicks].

Patrick Wood
102 · 2% · 521
0
Liked
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]