LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
close workbook without closing excel and stop recursive function chris Excel Discussion (Misc queries) 3 July 10th 06 08:23 PM
Recursive Functio help BigBobbo Excel Worksheet Functions 1 May 10th 06 07:23 PM
Recursive Function + File searching to return path ph8[_6_] Excel Programming 24 October 14th 05 08:45 AM
recursive sums Joe Excel Worksheet Functions 6 July 17th 05 09:45 AM
Recursive Subs? ExcelMonkey[_74_] Excel Programming 5 February 5th 04 02:54 AM


All times are GMT +1. The time now is 12:29 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"