Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Why doesn't this recursive function return the right value?
Have a recursive function that returns the window handle of a particular
window, based on the window type and text. It works fine and it finds the right window, but it just doesn't return the right value. It returns zero. When I do a msgbox before the Exit function it shows fine, or when I retrieve the value with a private variable I get the right value as well. It is not a problem, but just wondering why this happens. Option Explicit Private Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function GetWindow Lib "user32" _ (ByVal hwnd As Long, _ ByVal wCmd 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 GetClassName Lib "user32" _ Alias "GetClassNameA" _ (ByVal hwnd As Long, _ ByVal lpClassName As String, _ ByVal nMaxCount As Long) As Long Private Const GW_HWNDFIRST = 0 Private Const GW_HWNDLAST = 1 Private Const GW_HWNDNEXT = 2 Private Const GW_HWNDPREV = 3 Private Const GW_OWNER = 4 Private Const GW_CHILD = 5 Sub tester() MsgBox FindWindowHwndLike(0, "Static", "DOB", 0) End Sub Function FindWindowHwndLike(ByVal hWndStart As Long, _ ByVal ClassName As String, _ ByVal WindowTitle As String, _ ByVal level As Long) As Long 'finds the first window where the class name start with ClassName 'and where the Window title starts with WindowTitle, returns Hwnd '----------------------------------------------------------------- Dim hwnd As Long Dim sWindowTitle As String Dim sClassName As String Dim r As Long 'Initialize if necessary. This is only executed 'when level = 0 and hWndStart = 0, normally 'only on the first call to the routine. If level = 0 Then If hWndStart = 0 Then hWndStart = GetDesktopWindow() End If End If 'Increase recursion counter level = level + 1 'Get first child window hwnd = GetWindow(hWndStart, GW_CHILD) Do Until hwnd = 0 'Search children by recursion Call FindWindowHwndLike(hwnd, ClassName, WindowTitle, level) 'Get the window text sWindowTitle = Space$(255) r = GetWindowText(hwnd, sWindowTitle, 255) sWindowTitle = Left$(sWindowTitle, r) 'get the class name sClassName = Space$(255) r = GetClassName(hwnd, sClassName, 255) sClassName = Left$(sClassName, r) 'Check if the window matches the search parameters If (sWindowTitle Like "*" & WindowTitle & "*") And _ (sClassName Like ClassName & "*") Then FindWindowHwndLike = hwnd Exit Function End If 'Get next child window hwnd = GetWindow(hwnd, GW_HWNDNEXT) Loop End Function Thanks for any advice. RBS |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
close workbook without closing excel and stop recursive function | Excel Discussion (Misc queries) | |||
Recursive Functio help | Excel Worksheet Functions | |||
Recursive Function + File searching to return path | Excel Programming | |||
recursive sums | Excel Worksheet Functions | |||
Recursive Subs? | Excel Programming |