[RESOLVED] My code is working only when I toggle a breakpoint and STEP into code
The following code launches internet explorer on the google webpage. Then a routine confirms that the IE windows was opened AND found.
Code:
Call proLaunchIE
Public Sub proLaunchIE()
Shell "C:\Program Files\Internet Explorer\IEXPLORE.EXE www.google.com", vbMaximizedFocus
Dim booIEWasFound As Boolean
For i = 0 To 20
If fctIEHasFinishedLoading("Google") = True Then
'loading SUCCESSFUL
booIEWasFound = True
Exit For
End If
Next i
If booIEWasFound = False Then
MsgBox "String 'google' not found"
Exit Sub
End If
End Sub
Public Function fctIEHasFinishedLoading(strWindowCaption As String) As Boolean
Dim strWindowCaptionFound As String
Call proWaitWithSleep(1000)
strWindowCaptionFound = (GetCaption$(DLHFindWin&(frmForm, strWindowCaption, False)))
If strWindowCaptionFound <> "" Then
fctIEHasFinishedLoading = True
Else
Debug.Print "window-caption not found"
End If
End Function
Public Function GetCaption(lhWnd As Long) As String
Dim sA As String, lLen As Long
lLen& = GetWindowTextLength(lhWnd&)
sA$ = String(lLen&, 0&)
Call GetWindowText(lhWnd&, sA$, lLen& + 1)
GetCaption$ = sA$
DoEvents
End Function
Public Function DLHFindWin(frm As Form, WinTitle As String, _
CaseSensitive As Boolean) As Long
Dim lhWnd As Long, sA As String
lhWnd& = frm.hwnd
Do
DoEvents
If lhWnd& = 0 Then Exit Do
If CaseSensitive = False Then
sA$ = LCase$(GetCaption(lhWnd&))
WinTitle$ = LCase$(WinTitle$)
Else
sA$ = GetCaption(lhWnd&)
End If
If InStr(sA$, WinTitle$) Then
DLHFindWin& = lhWnd&
Exit Do
Else
DLHFindWin& = 0
End If
lhWnd& = GetNextWindow(lhWnd&, 2)
Loop
End Function
Public Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sClass As String * 255
Call GetClassName(hwnd, sClass, 255)
If Left(sClass, 4) = "Edit" Then
hwndCONTROL = hwnd
Exit Function 'the first "edit" field found is the one I need - I can quit function
End If
EnumProc = hwnd
End Function
This code will work ONLY if I toggle a breakpoint in the proLaunchIE procedure. I can simply CTRL-BREAK into code, then press F5 and it'll find the "google" IE window. But won't find it without user interaction. It will simply end the loop without finding IE. I think this is weird. Anyone with an idea what is causing that?
Tried putting "DoEvents" here and there but it didn't help.
Thank you!
Re: My code is working only when I toggle a breakpoint and STEP into code
no DoEvents to let others things to happen in background?, when your program is in IDE mode and you break it, you are letting others things happens in background..
Also you can't be sure it will load before the 21 cycles of that for/next, your approach is completely wrong, because it will be timming sensitive about what else is doing the computer.
you must lookup for SHELL AND WAIT function.
SHELL returns a PID if sucess.
Re: My code is working only when I toggle a breakpoint and STEP into code
Thanks for your reply!
I know this code is not very clean right now. This basic unoptimized code was to try to debug.
Obviously I wouldn't be doing such looping (21 cycles).
I did try with some DoEvents here and there but it didn't help.
Here's what I could notice, if this gives any idea:
When I launch my procedure, google fires up and my app doesn't trap it. I can close IE and relaunch procedure, the same will always happen.
BUT, if I leave a IE window opened, and RELAUNCH the procedure (a 2nd IE pops up) and my app will trap it instantly.
Bad loop in the caption-search? I doubt so, because if I jump in debug (ctrl-break) on the very first try, it'll trap it as well.
Re: My code is working only when I toggle a breakpoint and STEP into code
I have a found some alternative functions that just seemed to work, even without using a timer and going in a loop. Seems to be using GetDesktopWindow, which I never used before.
Working code:
https://www.experts-exchange.com/que...ease-help.html
Thank you!
Re: My code is working only when I toggle a breakpoint and STEP into code