PDA

View Full Version : Using VBA to detect a specific System Process



Banixxx
01-21-2008, 12:20 PM
OK so here is whats going on. I have this Code which works fine. It opens Avaya CMS Supervisor v11 and runs a report copies the Data to the clipboard and paste it to a Tab.

NOTE: Instance of Username , password, and Server have ben changed for confidentiallyity these are not issues.
Sub AvayaData()
'
' AvayaData Macro
' Macro recorded 1/20/2008 by ldorez
'
Dim cvsApp As New AVSUP.cvsApplication
Dim cvsConn As New cvsConnection
Dim cvsSrv As New cvsServer
Dim cvsCatalog As New cvsCatalog
Dim cvsRpt As New cvsReport
Dim avayaSheet As New Worksheet
Dim neededSheet As New Worksheet
Set avayaSheet = Sheets("Avaya")
Set neededSheet = Sheets("Needed")
Application.ScreenUpdating = False
If cvsApp.CreateServer("USERNAME", "PASSWORD", "", "SERVER", False, "ENU", cvsSrv, cvsConn) Then
If cvsConn.Login("USERNAME", "PASSWORD", "SERVER", "ENU") Then
End If
End If
Set cvsCatalog = cvsSrv.Reports
cvsSrv.Reports.ACD = 2
cvsCatalog.CreateReport cvsCatalog.Reports.Item("Real-Time\Agent\Agent Group Report"), cvsRpt
If cvsRpt.SetProperty("Agent Group", "All") Then
End If
cvsRpt.Run
cvsRpt.ExportData "", 9, 0, True, False, True
avayaSheet.Activate
Cells(2, 2).Select
ActiveCell.PasteSpecial (xlPasteAll)
neededSheet.Activate
StopReports:
cvsRpt.Quit
cvsSrv.Connected = False
Set cvsCatalog = Nothing
Set cvsRpt = Nothing
Set cvsSrv = Nothing
Set cvsApp = Nothing
Application.ScreenUpdating = False
'
End Sub
Function detectP()

End Function
End Function



I found this KB article written by:
Marcster (http://www.vbaexpress.com/forum/member.php?u=2411)
for termainateing a Specific System Process

Option Explicit

Sub TerminateProcess()
'---------------------------------------------------------------------------------------
' : Terminates a process. First checking to see if it is running or not.
' : Uses WMI (Windows Management Instrumentation) to query all running processes
' : then terminates ALL instances of the specified process
' : held in the variable strTerminateThis.
' :
' : ***WARNING: This will terminate a specified running process,use with caution!.
' : ***Terminating certain processes can effect the running of Windows and/or
' : ***running applications.
'---------------------------------------------------------------------------------------
Dim strTerminateThis As String 'The variable to hold the process to terminate
Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
Dim intError As Integer

strTerminateThis = "notepad.exe" 'Process to terminate,
'change notepad.exe to the process you want to terminate

Set objWMIcimv2 = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2") 'Connect to CIMV2 Namespace

Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='" & strTerminateThis & "'") 'Find the process to terminate


If objList.Count = 0 Then 'If 0 then process isn't running
MsgBox strTerminateThis & " is NOT running." & vbCr & vbCr _
& "Exiting procedure.", vbCritical, "Unable to continue"
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
Exit Sub
Else
'Ask if OK to continue
Select Case MsgBox("Are you sure you want to terminate this running process?:" _
& vbCrLf & "" _
& vbCrLf & "Process name: " & strTerminateThis _
& vbCrLf & "" _
& vbCrLf & "Note:" _
& vbCrLf & "Terminating certain processes can effect the running of Windows" _
& "and/or running applications. The process will terminate if you OK it, WITHOUT " _
& "giving you the chance to save any changes in anything that is running in the specified process above." _
, vbOKCancel Or vbQuestion Or vbSystemModal Or vbDefaultButton1, "WARNING:")

Case vbOK
'OK to continue with terminating the process
For Each objProcess In objList

intError = objProcess.Terminate 'Terminates a process and all of its threads.
'Return value is 0 for success. Any other number is an error.
If intError <> 0 Then
MsgBox "ERROR: Unable to terminate that process.", vbCritical, "Aborting"
Exit Sub
End If
Next
'ALL instances of specified process (strTerminateThis) has been terminated
Call MsgBox("ALL instances of process " & strTerminateThis & " has been successfully terminated.", _
vbInformation, "Process Terminated")

Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
Exit Sub

Case vbCancel
'NOT OK to continue with the termination, abort
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
Exit Sub
End Select
End If

End Sub


I can modify this code to work with AVSUP.exe but What i truely want is for it to dected if the Avaya Application is running and if so you that Process/Object. I essentailly want to get rid of this



Dim cvsApp As New AVSUP.cvsApplication
Dim cvsConn As New cvsConnection

If cvsApp.CreateServer("USERNAME", "PASSWORD", "", "SERVER", False, "ENU", cvsSrv, cvsConn) Then
If cvsConn.Login("USERNAME", "PASSWORD", "SERVER", "ENU") Then
End If

And get grab the object and set its properties like Server and Run report. Is this possible

malik641
01-21-2008, 08:42 PM
I'm sorry, but you kind of lost me here:

I can modify this code to work with AVSUP.exe but What i truely want is for it to dected if the Avaya Application is running and if so you that Process/Object.
Huh?


If I interpret your post correctly, you want to get a running instance of the Avaya application (if one is running at the moment). You can achieve this by using 'Late Binding' in your code. Right now, you have set a reference to the Avaya library and you are using "New" to create an instance (this is 'Early Binding').

Check out the GetObject() function here (http://msdn2.microsoft.com/en-us/library/e9waz863%28VS.80%29.aspx).
[EDIT: Sorry, that code is a LITTLE different because it's VB.NET. Check out THIS ARTICLE for VBA (http://msdn2.microsoft.com/en-us/library/aa164798%28office.10%29.aspx).]

Try grabbing an instance using this (late binding) function. Hope this helps.

Oh and by the way, you should probably use the method you have been using just so you have Intellisense while coding, then convert it to late binding.

rory
01-22-2008, 07:31 AM
You can still use early binding with GetObject incidentally - the function itself has nothing to do with the binding. Just leave the declaration as it is but use GetObject rather than declaring a New instance.

malik641
01-22-2008, 08:04 AM
You can still use early binding with GetObject incidentally - the function itself has nothing to do with the binding. Just leave the declaration as it is but use GetObject rather than declaring a New instance.
You're right. Thanks Rory. I never thought of it that way :)

Marcster
01-22-2008, 11:21 AM
What i truely want is for it to dected if the Avaya Application is running



Sub IsItRunning()
strComputer = "."
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set proc = objWMI.ExecQuery _
("Select * from Win32_Process Where Name = 'AvayaApplication.exe'")
If proc.Count = 0 Then
MsgBox "AvayaApplication.exe is not running."
Else
MsgBox "AvayaApplication.exe is running."
End If
End Sub



Marcster.

Banixxx
01-22-2008, 03:03 PM
Ok i think i have confused everyone. I know how to detect is a application is running using marcster's above and KB Code for terming a process.

Lets use a different example and try to get the same results.

EXAMPLE:
I want VBA to detect if an Instance of IE is running "iexplorer.exe" and if not start it and go to a specific URL lets say. But the major part if if IE is already running i just want to grab the object/application and manipulate it by somethine like GetObject(iexplore.exe) and then use it properties to change it like send it to google.

Now the example it made up only to help understand what im doing since you all may not know what Avaya is. I want to grab the AVSUP.exe application that running and uses it properties like "createServer()" etc..

rory
01-22-2008, 03:28 PM
What makes you think we are confused? We told you to use GetObject. Assuming your original code works, you would need something like:
Set cvsApp = Getobject("", "AVSUP.cvsApplication")
you may also be able to grab a lower level object if necessary - I don't know the program or its object model.

Banixxx
01-22-2008, 04:20 PM
What makes you think we are confused? We told you to use GetObject. Assuming your original code works, you would need something like:
Set cvsApp = Getobject("", "AVSUP.cvsApplication")
you may also be able to grab a lower level object if necessary - I don't know the program or its object model.

Rory, sorry i didnt mean to come across rude if i did. This works fine now i just need to modify this to detect if the object is running and then grab it and if not create it. I thnk i should be able to handle this if not ill be back. Thanks a bunch everyone