[AccessD] Code to detect if Outlook is open

MartyConnelly martyconnelly at shaw.ca
Mon Mar 7 10:29:43 CST 2005


Here is some rough code I use to determine all the names of  tasks running.
I don't know if different version of Outlook may have slightly different 
names

Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2

Private Declare Function GetWindow Lib "user32" _
   (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetParent Lib "user32" _
   (ByVal hWnd As Long) As Long
Private Declare Function GetWindowTextLength Lib _
   "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowText Lib "user32" _
   Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal _
   lpString As String, ByVal cch As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetFocusAPI Lib "user32" Alias "SetFocus" _
   (ByVal hWnd As Long) As Long
'Determining Which Tasks Are Running

'With the Microsoft Windows operating system,
'you can run any number of applications simultaneously.
'Occasionally, you may need to determine which tasks are currently being
'run.
'This can be accomplished by using several Windows application programming
'interface
'(API) functions.
'To find the names of all currently executing tasks,
'you must first determine the handle of the window that is currently
'at the top of the z-order. This, of course, would be the window of your
'own Microsoft Visual Basic application.
'You can use the Windows API GetWindow function to retrieve the handle
'of your application's window with the statement:

'    CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)
' To use in Access replace with the following
'     parent_hwnd = FindWindow(vbNullString, "Microsoft Access")
'The first argument of the GetWindow function is the handle of the window
'that is at the top of the z-order. In this case, this is the handle of
'Form1.

'The second argument of the GetWindow function specifies the window
'you want to retrieve the handle for.
'This argument can have one of the following values:

'  GW_CHILD Retrieve the handle for the child window.
'  GW_HWNDFIRST Retrieve the handle for the window at the top of the z-
'order.
'  GW_HWNDLAST Retrieve the handle for the window at the bottom of the z-
'order.
'  GW_HWNDNEXT Retrieve the handle of the window below the specified window
'in the z-order.
'  GW_HWNDPREV Retrieve the handle of the window above the specified window
'in the z-order.
'  GW_OWNER Retrieve the handle of the window that owns the specified
'window, if any.

'After you have retrieved the application's window handle,
'you can use the Windows API GetParent function to retrieve this window's
'child window handle. Next, you call the Windows API GetWindowText and
'GetWindowTextLength functions to retrieve the text in the window's title
'bar
'and the length of this text, respectively. You can then use the text string
'in your own application. For example, you can save the title bar text
'to a List Box control.

'All of the above steps are repeated until you have processed all running
'tasks.
'You know that you have gone through each task when the current window is
'that'of your own application.


Function LoadTaskList() As String
Dim CurrWnd As Long
Dim Length As Long
Dim TaskName As String
Dim Parent As Long
Dim parent_hwnd As Long
Dim strMyTaskList As String
  strMyTaskList = " Task List " & vbCrLf

' This line below works from VB form
'CurrWnd = GetWindow(Form1.hwnd, GW_HWNDFIRST)
' get Parent Window Handle
     parent_hwnd = FindWindow(vbNullString, "Microsoft Access")
    If parent_hwnd = 0 Then
     MsgBox "Access Not Found"
      Exit Function
     End If
      'SetFocusAPI parent_hwnd
       CurrWnd = parent_hwnd
While CurrWnd <> 0
  Parent = GetParent(CurrWnd)
  Length = GetWindowTextLength(CurrWnd)
  TaskName = Space$(Length + 1)
  Length = GetWindowText(CurrWnd, TaskName, Length + 1)
  TaskName = Left$(TaskName, Len(TaskName) - 1)

  If Length > 0 Then
  'If TaskName <> Me.Caption Then
  'If TaskName <> "Microsoft Access" Then
     'List1.AddItem TaskName
     strMyTaskList = strMyTaskList & TaskName & vbCrLf
     Debug.Print TaskName
   'End If
  End If
  CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)

    DoEvents
Wend
LoadTaskList = strMyTaskList
End Function

Mike & Doris Manning wrote:

>Thanks, Andy, but that isn't quite what I was looking for.  I had some API
>code I wrote for an AccessXP database that could detect whether Outlook was
>already open and process accordingly.  I used API code because if Outlook
>was already open on the user's machine, I didn't want my code to close it.
>My old code used the EnumWindows process to look through the tabs but when I
>run it using Access2003, I don't get the same results.  I was hoping someone
>had updated code.
>
>Doris Manning
>Database Administrator
>Hargrove Inc.
>www.hargroveinc.com
>
>
>-----Original Message-----
>From: accessd-bounces at databaseadvisors.com
>[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Andy Lacey
>Sent: Monday, March 07, 2005 8:53 AM
>To: Access Developers discussion and problem solving
>Subject: Re: [AccessD] Code to detect if Outlook is open
>
>
>Hi Doris
>I can't test it in A2003 but this works in earlier versions
>
>Dim objOutlook As Outlook.Application
>
>On Error GoTo Err_NoOutlook
>Set objOutlook = GetObject(, "Outlook.Application")
>
>
>If it errors then Outlook's not running.
>
>Any use?
>
>--
>Andy Lacey
>http://www.minstersystems.co.uk
>
>
>
>--------- Original Message --------
>From: Access Developers discussion and problem solving
><accessd at databaseadvisors.com>
>To: 'Access Developers discussion and problem solving'
><accessd at databaseadvisors.com>
>Subject: [AccessD] Code to detect if Outlook is open
>Date: 07/03/05 14:33
>
>  
>
>>Does anyone have any code to detect if Outlook is open that works in 
>>Access2003?
>>
>>Doris Manning
>>Database Administrator
>>Hargrove Inc.
>>www.hargroveinc.com
>>
>>
>>--
>>AccessD mailing list
>>AccessD at databaseadvisors.com 
>>http://databaseadvisors.com/mailman/listinfo/accessd
>>Website: http://www.databaseadvisors.com
>>
>>
>>
>>
>>
>>    
>>
>
>________________________________________________
>Message sent using UebiMiau 2.7.2
>
>  
>

-- 
Marty Connelly
Victoria, B.C.
Canada






More information about the AccessD mailing list