Rick has brought this to us :
I have an app written in another language but I thought folks here
might be able to help me out with something related to MS Word. In my
app I need to determine if MS Word is open. We've done this for years
using the FindWindowA() API call inside user32.dll, giving it the
string "OpusApp" --
retval = FindWindowA("OpusApp", [nullvalue])
and it's worked fine -- if I get a positive integer back I know Word
is open. But now using Windows 7 (32-bit) I'm getting a positive
value back even if Word is open. Any thoughts? Thanks.
Here's a routine that will search the list of all running processes for an
executable with a given filename. Paths are not considered in this case.
I've indented it here, to illuminate linewrap.
' Process info API declarations - 9x/NT5+
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal
dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As
Long, ByRef lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As
Long, ByRef lppe As PROCESSENTRY32) As Long
' Toolhelp constants.
Private Const TH32CS_SNAPPROCESS As Long = &H2&
Private Const MAX_PATH As Long = 260
' Uncover process information on 9x/NT5+.
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 * MAX_PATH
End Type
Public Function ExeRunning(ByVal ExeName As String) As Boolean
Dim hSnap As Long
Dim ProcEntry As PROCESSENTRY32
Dim ThisProc As String
' Use the documented methods...
' Make sure we have the needed function.
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&)
If hSnap Then
ProcEntry.dwSize = Len(ProcEntry)
' Iterate through the processes once to find the parent:
If Process32First(hSnap, ProcEntry) Then
Do
ThisProc = Left$(ProcEntry.szExeFile,
InStr(ProcEntry.szExeFile, vbNullChar) - 1)
If StrComp(ThisProc, ExeName, vbTextCompare) = 0 Then
ExeRunning = True
Exit Do
End If
Loop While Process32Next(hSnap, ProcEntry)
End If
End If
End Function
For your situation, you'd call it like this:
If ExeRunning("winword.exe") Then
That help?