Using VBA I'm trying to loop through and determine the programs which are currently loaded.
The reason for this is that I'm attempting to wait until a loaded program ends before I do something.
I found two different ways of checking for a specific loaded program.
1.
Private Sub Command0_Click()
Dim strTemp As String
strTemp = "excel.application"
If IsAppRunning(strTemp) Then
MsgBox "pgm loaded"
Else
MsgBox "NOT loaded"
End If
End Sub
' Generalized Function to Check if an Instance of Application is running in the machine
Function IsAppRunning(ByVal sAppName) As Boolean
Dim oApp As Object
On Error Resume Next
Set oApp = GetObject(, sAppName)
If Not oApp Is Nothing Then
Set oApp = Nothing
IsAppRunning = True
End If
End Function
This works when I look for MS programs, but the program which I want to look for is not found, so I'm thinking that I'm not naming it correctly. Thus the desire to loop through and determine which programs are loaded and I'll be able to use this method of
determining if the program is loaded. Or if the Task Manager (or other pgm), displays the names of the loaded programs, then I wouldn't need to programmatically determine all loaded programs.
2.
Private Sub Command0_Click()
Dim strTemp As String
strTemp = "msaccess.exe"
If pgmLoaded(strTemp) Then
MsgBox "pgm loaded"
Else
MsgBox "NOT loaded"
End If
End Sub
Function pgmLoaded(ByVal strApp) As Boolean
Dim objWMIcimv2 As Object
Dim objList As Object
Set objWMIcimv2 = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\.\root\cimv2") 'Connect to CIMV2 Namespace
Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='" & strApp & "'") 'Find the process to terminate
If objList.Count = 0 Then 'If 0 then process isn't running
pgmLoaded = False
Else
pgmLoaded = True
End If
Set objWMIcimv2 = Nothing
Set objList = Nothing
End Function
This method works for the program which I'm looking for, but it must be checking the processes in memory and not the programs. Unfortunately, the program I'm looking for leaves processes in memory after it closes, therefore making this method unworkable
for me.
Option 1 seems to be the method I want, but I can't figure out how to assign strTemp with the correct value for the program I'm looking for (National Instruments DIAdem)