Here's part of a project I did once.
VB Code:
  1. Option Explicit
  2.  
  3. Private Declare Function GetWindowTextLength Lib _
  4.   "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
  5. Private Declare Function GetWindowText Lib "user32" _
  6.   Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal _
  7.   lpString As String, ByVal cch As Long) As Long
  8. Private Declare Function GetWindow Lib "user32" _
  9. (ByVal hWnd As Long, ByVal wCmd As Long) As Long
  10.  
  11. Private Const GW_HWNDFIRST = 0
  12. Private Const GW_HWNDNEXT = 2
  13.  
  14. Private Function GetHandle(Caption As String, TaskName As String) As Long
  15.     Dim CurrWnd As Long
  16.     Dim e0 As String
  17.     Dim Length As Integer
  18.     Dim tName As String
  19.     Dim Pos As Integer
  20.    
  21.     CurrWnd = GetWindow(Me.hWnd, GW_HWNDFIRST)
  22.     e0 = UCase$(Caption)
  23.     Do While CurrWnd <> 0
  24.         Length = GetWindowTextLength(CurrWnd)
  25.         tName = Space$(Length + 1)
  26.         Length = GetWindowText(CurrWnd, tName, Length + 1)
  27.         tName = Left$(tName, Len(tName) - 1)
  28.         If Length > 0 Then
  29.             If InStr(UCase$(Caption), "*") Then
  30.                 e0 = Right$(UCase$(Caption), Len(Caption) - 1)
  31.                 Pos = InStr(UCase$(tName), e0)
  32.                 If Pos Then
  33.                     'The wanted one
  34.                     TaskName = tName
  35.                     Exit Do
  36.                 End If
  37.             Else
  38.                 If UCase$(Caption) = UCase$(tName) Then
  39.                     TaskName = tName
  40.                     Exit Do
  41.                 End If
  42.             End If
  43.         End If
  44.         CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
  45.         DoEvents
  46.     Loop
  47.    
  48.     GetHandle = CurrWnd
  49. End Function
  50.  
  51. Private Sub Form_Load()
  52.     Dim hWnd As Long
  53.     Dim TaskName As String
  54.    
  55.     hWnd = GetHandle("* - Microsoft Outlook", TaskName)
  56.     MsgBox "The wanted window is " & TaskName
  57. End Sub

In this example, it doesn't mind if your on the Inbox, Outbox or whatever... it will always find the Outlook's Window -if opened-