[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Check to see if application is running

Posted on 2004-11-04
12
Medium Priority
?
176 Views
Last Modified: 2010-05-02
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.
0
Comment
Question by:Mike Miller
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 48

Expert Comment

by:Mikal613
ID: 12498119
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
 
LVL 8

Expert Comment

by:ampapa
ID: 12498126
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12499959
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 48

Expert Comment

by:Mikal613
ID: 12500023
its not good habits to force errors
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12500057
It doesn't hurt the computer.
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12503627
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 12504216
now you have to loop through every process?
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 1000 total points
ID: 12504438
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
 
LVL 48

Expert Comment

by:Mikal613
ID: 12504469
much better Eddy ;)
0
 

Expert Comment

by:Maintex
ID: 12519468
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12746701
should work on windows 2000 or higher
0
 
LVL 48

Expert Comment

by:Mikal613
ID: 12746719
Congrats EDDY  Fine job;
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month19 days, 1 hour left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question