Check if application is running (1 Viewer)

beginner123

Registered User.
Local time
Yesterday, 21:02
Joined
Apr 13, 2013
Messages
44
Hello,

I am trying to check if an application is running, for my example I am using notepad although this is not the final application to be checked.

Private Sub btncheck_Click()
Dim Notepad As Object

On Error Resume Next
Set Notepad = GetObject(, "notepad.Application")
On Error GoTo 0

If Notepad Is Nothing Then
MsgBox "Notepad is not open, open notepad and try again"
Else
'Do something else
End If
End Sub

This doesn't work, always displays message. Have also tried an API call after googling but no joy.

Has anyone a basic example or link they could share.

Many Thanks
Rob
 

TJPoorman

Registered User.
Local time
Yesterday, 22:02
Joined
Jul 23, 2013
Messages
402
A quick google search came up with this:

Code:
Public Function exampleIsProcessRunning()  
    Debug.Print IsProcessRunning("Explorer.EXE")
    Debug.Print IsProcessRunning("NOTE RUNNING.EXE")
End Function

Public Function IsProcessRunning(process As String)
    Dim objList As Object

    Set objList = GetObject("winmgmts:") _
        .ExecQuery("select * from win32_process where name='" & process & "'")

    If objList.Count > 0 Then
        IsProcessRunning = True
    Else
        IsProcessRunning = False
    End If
End Function

The original is here.
 

MarkK

bit cruncher
Local time
Yesterday, 21:02
Joined
Mar 17, 2004
Messages
8,181
Nice. I would always change this block . . .
Code:
    If objList.Count > 0 Then
        IsProcessRunning = True
    Else
        IsProcessRunning = False
    End If
. . . to . . .
Code:
    IsProcessRunning = objList.Count > 0
 

TJPoorman

Registered User.
Local time
Yesterday, 22:02
Joined
Jul 23, 2013
Messages
402
Nice. I would always change this block . . .
Code:
    If objList.Count > 0 Then
        IsProcessRunning = True
    Else
        IsProcessRunning = False
    End If
. . . to . . .
Code:
    IsProcessRunning = objList.Count > 0

As would I, I just left it this way to better illustrate how it's working for those that have trouble reading code that way.
 

beginner123

Registered User.
Local time
Yesterday, 21:02
Joined
Apr 13, 2013
Messages
44
Many thanks to you both for the solution, much appreciated.
Rob
 

Users who are viewing this thread

Top Bottom