Say you have notepad.exe open but want to know what the title bar says. Like: “TEST.txt – Notepad” I’ve found lots of stuff out there on changing the title in Excel and Access but I just want to monitor a programs caption or title.
Also found some script that will let you see if the program is running.
---Put this in a Project_User Module
Private Declare Function EnumProcesses Lib "psapi.dll" _
(ByRef lpidProcess As Long, ByVal cb As Long, _
ByRef cbNeeded As Long) As Long
Private Declare Function OpenProcess Lib "Kernel32.dll" _
(ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
ByVal dwProcId As Long) As Long
Private Declare Function EnumProcessModules Lib "psapi.dll" _
(ByVal hProcess As Long, ByRef lphModule As Long, _
ByVal cb As Long, ByRef cbNeeded As Long) As Long
Private Declare Function GetModuleFileNameExA Lib "psapi.dll" _
(ByVal hProcess As Long, ByVal hModule As Long, _
ByVal strModuleName As String, ByVal nSize As Long) As Long
Private Declare Function CloseHandle Lib "Kernel32.dll" _
(ByVal Handle As Long) As Long
Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_VM_READ = 16
Private Const MAX_PATH = 260
Public Function CheckForProcByExe(pEXEName As String) As Boolean
On Error Resume Next
Dim cb As Long
Dim cbNeeded As Long
Dim NumElements As Long
Dim lProcessIDs() As Long
Dim cbNeeded2 As Long
Dim lNumElements2 As Long
Dim lModules(1 To 200) As Long
Dim lRet As Long
Dim strModuleName As String
Dim nSize As Long
Dim hProcess As Long
Dim i As Long
'Get the array containing the process id's for each process object
cb = 8
cbNeeded = 96
Do While cb <= cbNeeded
cb = cb * 2
ReDim lProcessIDs(cb / 4) As Long
lRet = EnumProcesses(lProcessIDs(1), cb, cbNeeded)
Loop
NumElements = cbNeeded / 4
For i = 1 To NumElements
'Get a handle to the Process
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
Or PROCESS_VM_READ, 0, lProcessIDs(i))
'Got a Process handle
If hProcess <> 0 Then
'Get an array of the module handles for the specified
'process
lRet = EnumProcessModules(hProcess, lModules(1), 200, _
cbNeeded2)
'If the Module Array is retrieved, Get the ModuleFileName
If lRet <> 0 Then
strModuleName = Space(MAX_PATH)
nSize = 500
lRet = GetModuleFileNameExA(hProcess, lModules(1), _
strModuleName, nSize)
strModuleName = Left(strModuleName, lRet)
'Check for the client application running
If InStr(UCase(strModuleName), UCase(pEXEName)) Then
CheckForProcByExe = True
Exit Function
Else
CheckForProcByExe = False
End If
End If
End If
'Close the handle to the process
lRet = CloseHandle(hProcess)
Next
End Function
---Then this on a button or an event:
Private Sub CommandButton2_Click()
If CheckForProcByExe("teledac.exe") Then
MsgBox "Application is Running"
Else
MsgBox "Application is -*NOT*- Running."
End If
End Sub