Here's part of a project I did once.VB Code:
Option Explicit 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 GetWindow Lib "user32" _ (ByVal hWnd As Long, ByVal wCmd As Long) As Long Private Const GW_HWNDFIRST = 0 Private Const GW_HWNDNEXT = 2 Private Function GetHandle(Caption As String, TaskName As String) As Long Dim CurrWnd As Long Dim e0 As String Dim Length As Integer Dim tName As String Dim Pos As Integer CurrWnd = GetWindow(Me.hWnd, GW_HWNDFIRST) e0 = UCase$(Caption) Do While CurrWnd <> 0 Length = GetWindowTextLength(CurrWnd) tName = Space$(Length + 1) Length = GetWindowText(CurrWnd, tName, Length + 1) tName = Left$(tName, Len(tName) - 1) If Length > 0 Then If InStr(UCase$(Caption), "*") Then e0 = Right$(UCase$(Caption), Len(Caption) - 1) Pos = InStr(UCase$(tName), e0) If Pos Then 'The wanted one TaskName = tName Exit Do End If Else If UCase$(Caption) = UCase$(tName) Then TaskName = tName Exit Do End If End If End If CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT) DoEvents Loop GetHandle = CurrWnd End Function Private Sub Form_Load() Dim hWnd As Long Dim TaskName As String hWnd = GetHandle("* - Microsoft Outlook", TaskName) MsgBox "The wanted window is " & TaskName 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-




image).
Reply With Quote