Check to see if application is running

In Visual Basic 6:
How to I check to see if Outlook is running on a PC?
When a user clicks on a command button, I want the VB app to check for an
instance of Outlook that is running. If it is, then the program will continue
normally. If not, the message box will be displayed.
Mike MillerSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mikal613Commented:
Try the code below:

Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long

Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long

Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long

Private Type PROCESSENTRY32
   dwSize As Long
   cntUsage As Long
   th32ProcessID As Long
   th32DefaultHeapID As Long
   th32ModuleID As Long
   cntThreads As Long
   th32ParentProcessID As Long
   pcPriClassBase As Long
   dwFlags As Long
   szExeFile As String * 260
End Type

Private Const TH32CS_SNAPPROCESS = &H2&
Private Const hNull = 0

Private Sub Command1_Click()

    If ExeRunning("outlook.exe") Then
        MsgBox "Running"
    Else
        MsgBox "Not Running"
    End If

End Sub

Private Function ExeRunning(ByVal ExeFind As String) As Boolean

    Dim hProc As Long, ExeName As String
    Dim lRet As Long, Proc As PROCESSENTRY32
   
    hProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
   
    ExeRunning = False
   
    If hProc = 0 Then
        Exit Function
    End If
   
    Proc.dwSize = Len(Proc)
    lRet = Process32First(hProc, Proc)
   
    Do While lRet
   
        ExeName = Left(Proc.szExeFile, Len(Proc.szExeFile) - 1)
        If InStr(1, UCase(Trim(ExeName)), UCase(Trim(ExeFind))) Then
            ExeRunning = True
            Exit Function
        End If
       
        lRet = Process32Next(hProc, Proc)
   
    Loop

End Function
0
Erick37Commented:
Or this...

Private Sub Command1_Click()

    Dim oOutlook As Object
   
    On Error Resume Next
   
    Set oOutlook = GetObject(, "Outlook.Application")
   
    If Err.Number <> 0 Then
        MsgBox "Outlook not running, dude", vbInformation, Err.Description
    Else
        Set oOutlook = Nothing
    End If
   
    Err.Clear

End Sub
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mikal613Commented:
its not good habits to force errors
0
Erick37Commented:
It doesn't hurt the computer.
0
EDDYKTCommented:
Private Sub Form_Load()
   MsgBox FindProcess("outlook.exe") ' Your exefile here
End Sub

Function FindProcess(Process) As Boolean
   Set objWMIService = GetObject("winmgmts:")
   Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process")
   For Each objProcess In colProcesses
       If LCase(Process) = LCase(objProcess.Caption) Then FindProcess = True
   Next
End Function
0
Mikal613Commented:
now you have to loop through every process?
0
EDDYKTCommented:
To simpfy


Private Sub Form_Load()
   MsgBox FindProcess("outlook.exe") ' Your exefile here
End Sub

Function FindProcess(Process) As Boolean
   Set objWMIService = GetObject("winmgmts:")
   Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process where name='" & Process & "'")
      If (colProcesses.Count > 0) Then FindProcess = True
End Function
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mikal613Commented:
much better Eddy ;)
0
MaintexCommented:
Hi,

I am not helping here (maybe I am) but I have a query about these solutions, what versions of windows will that solution (querying the WMI Service) work with?
0
EDDYKTCommented:
should work on windows 2000 or higher
0
Mikal613Commented:
Congrats EDDY  Fine job;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.