Following is a simple standard function to retrieve the handle of a button inside any open window for access in an other running application.
As You can see I inserted two MsgBoxes at significant places. The function is running well as long as the MsgBoxes are not commented out. As soon as they are only one item the loops is handled, all others are overridden. How can I insert a delay or anything else providing a smooth working of the function?
As You can see I inserted two MsgBoxes at significant places. The function is running well as long as the MsgBoxes are not commented out. As soon as they are only one item the loops is handled, all others are overridden. How can I insert a delay or anything else providing a smooth working of the function?
Code:
'------------------------------------------------------------------------------------------------------- ' FUNCTION FindButtonInWindow '------------------------------------------------------------------------------------------------------- Function FindButtonInWindow(sWndTitle As String, sBtnText As String) As Dword 'INPUT: sWndTitle as Titel of the Window, sBtnText as Text of the Button 'OUTPUT: Handle of the found button as [Dword] On Error Resume Next Dim Btn As Long, CurHwnd As Dword, szT As Asciiz * 128 Dim Length As Long, x As Long, y As Long Function = 0 ' Trying to find the handle of the View Code Button so that ' by clicking this program's button, we can see the code ' window for this form. CurHwnd = GetDesktopWindow() 'Get Desktop handle CurHwnd = GetWindow(CurHwnd, %GW_Child) 'Find Child Windows of Desktop Do If CurHwnd = 0 Then Exit Do 'No (more) matches found ' Find out how long the text in this window is Length = GetWindowTextLength(CurHwnd) Length = GetWindowText(CurHwnd, szT, Length + 1) If InStr(szT, sWndTitle) Then MsgBox "" ' The value of sWindowTitle was found in this Window's text CurHwnd = GetWindow(CurHwnd, %GW_Child) ' Looking now for the Window's child windows Do If CurHwnd = 0 Then Exit Function 'No (more) matches found ' Find out how long the text in this window is Length = GetWindowTextLength(CurHwnd) Length = GetWindowText(CurHwnd, szT, Length + 1) If InStr(szT, sBtnText) Then MsgBox "" ' This is the handle we want '### Click(CurHwnd) 'alternatively click the Button Function = CurHwnd Exit Function 'Exit the Sub End If CurHwnd = GetWindow(CurHwnd, %GW_HwndNext) 'Keep looking Loop End If CurHwnd = GetWindow(CurHwnd, %GW_HwndNext) 'Keep looking Loop End Function
Comment