Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check outlook is running

Status
Not open for further replies.

MathiasB

Programmer
Aug 21, 2019
16
GB
Hi all I have a function that works well most of the time.

I have no clue who wrote it, it just works. The start looks like this:

IsExeRunning()

oLocator = CreateObject('WBEMScripting.SWBEMLocator')
oWMI = oLocator.ConnectServer()
oWMI.Security_.ImpersonationLevel = 3 && Impersonate
oProcesses = oWMI.ExecQuery([Select * From Win32_Process WHERE Name = '] + tcName + ['])

But of late with new or classic outlook we are finding that it doesn't always work. I would like to enhance it so it can find other derivatives of outlook whatever they're called. If you've done something close please, advice will much appreciated.

K R

Mathias
 
You could try, if you get other/more results with https://github.com/VFPX/Win32API/blob/master/samples/sample_162.md
Or you just compare the unfiltered result of Select * From Win32_Process with the result after starting the Outlook variant that slips when only looking for whatever your tcName values are.

My own observations are: The WMI lists all process with all properties of Win32_Process WMI objects, which are plenty. The EnumProcesses API based code in the first place only gets some number of processes, most important process ID, then gets more details from EnumProcessModules, which for example gets all involved DLLs listed. But the Moduleslist I get is only for a few processes, seems there's more to get from elsewhere and I'd stick to the WMI Win32_Process list, which I think is sufficient.

All in all you can identify a process by the exe filename, so the question you have can be condensed to the question which exe file names all outlook versions have. My guess is in later Office or Microsoft365 versions the exe name may not be outlook or contain outlook anymore, if outlook web is used you only will see an entry of Edge, Firefox, Chrome or whatever standard browser, perhaps. You get more from looking at the property CommandLine than just Name or Caption.

For example, having started VFP9 normally and by creating a VisualFoxPro.Application OLE automation server the CommandLine property of the first vfp9 process is just the exe file, the OLE started version has commandline parameters ...vfp9.exe /automation -Embedding, so you can differentiate between VFP exe started via Automation or not.
 
Last edited:
Hello Mathias,

i wrote a function to find a window by parts of its caption, for example Adobe Reader or word which have the active filename in its caption, so this may help

Code:
Declare Integer FindWindow In Win32API String @cClassName, String @cWindowName
Declare Integer GetActiveWindow  In Win32API
Declare Integer GetWindow In WIN32API Integer HWnd, Integer uCmd
Declare Integer GetWindowText In WIN32API Integer HWnd,   String @lpString,   Integer nMaxCount

? api_getwindowbyname("Outlook") && 0 = not found


Function api_getwindowbyname(cName)
  Local lnfound,lcBuffer,lnoldhwnd,lnhWnd,lnretval
  lnfound = findwindow(0,cName)
  If lnfound>0
    Return lnfound
  Endif

  lnoldhwnd = GetActiveWindow()
  lnhWnd = GetWindow(lnoldhwnd, 0) && First
  Do While lnhWnd <> 0
    lcBuffer = Replicate(Chr(0), 255)
    lnretval = GetWindowText(lnhWnd, @lcBuffer, Len(lcBuffer))
    If lnretval > 0 Then
      lcBuffer =  Upper(Left(lcBuffer,lnretval))
      If !Empty(At(Upper(cName),lcBuffer))
        lnfound = lnhWnd
        Exit
      Endif
    Endif
    lnoldhwnd = lnhWnd
    lnhWnd = GetWindow(lnoldhwnd, 2) 
  Enddo
  Return lnfound
Endfunc
 
Hello Mathias,

i wrote a function to find a window by parts of its caption, for example Adobe Reader or word which have the active filename in its caption, so this may help

Code:
Declare Integer FindWindow In Win32API String @cClassName, String @cWindowName
Declare Integer GetActiveWindow  In Win32API
Declare Integer GetWindow In WIN32API Integer HWnd, Integer uCmd
Declare Integer GetWindowText In WIN32API Integer HWnd,   String @lpString,   Integer nMaxCount

? api_getwindowbyname("Outlook") && 0 = not found


Function api_getwindowbyname(cName)
  Local lnfound,lcBuffer,lnoldhwnd,lnhWnd,lnretval
  lnfound = findwindow(0,cName)
  If lnfound>0
    Return lnfound
  Endif

  lnoldhwnd = GetActiveWindow()
  lnhWnd = GetWindow(lnoldhwnd, 0) && First
  Do While lnhWnd <> 0
    lcBuffer = Replicate(Chr(0), 255)
    lnretval = GetWindowText(lnhWnd, @lcBuffer, Len(lcBuffer))
    If lnretval > 0 Then
      lcBuffer =  Upper(Left(lcBuffer,lnretval))
      If !Empty(At(Upper(cName),lcBuffer))
        lnfound = lnhWnd
        Exit
      Endif
    Endif
    lnoldhwnd = lnhWnd
    lnhWnd = GetWindow(lnoldhwnd, 2)
  Enddo
  Return lnfound
Endfunc
This is great Tom, Thank you so much. I will put it to test and roll it out there where the jury is. Much appreciated sir!
 
Tom,

good idea, it could be more stable to find outlook in the window title than in the file/process name.

I remember that FindWindow can also find a window by class, and Word Windows are based on the class name OpusApp, which I think also is true for outlook windows. So it can't be used as a primary search, but could be made a secondary indicator of a Office Outlook Window and not just any window that for any reason has the word "outlook" in it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top