You are on page 1of 4

Resolved [RESOLVED] Webbrowser - loading document complete more then one time

hello
i have a question am making an update checker with a webbrowser to check if a
update to my program is available thru the webbrowsers webpages text the problem
that i am having i am getting a ton of msgboxes for my update status in the
document complete as if the webbrowser is not complete keeps firing few times i
want only one msgbox to appear any ideas on this?

Code:
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, url As Variant)

If InStr(WebBrowser1.Document.body.outerText, "Install.exe") > 0 Then


Dim NewVersion As String
NewVersion = Trim(NthField(NthField(WebBrowser1.Document.body.outerText,
"Install.exe", 2), "Browser", 2))

If NewVersion = AppVersion Then


MsgBox "No update available you have the latest version!"
'i tryed ExitSub still showing multiple msgboxes
Exit Sub

ElseIf NewVersion = AppVersion + 1 Then


Select Case MsgBox("Update available would you like to download now?", vbYesNo)

Case vbYes
ShowURL ("UpdateLink")

Case vbNo
Exit Sub
End Select
End If

End Sub
Last edited by sinner0636; Apr 19th, 2015 at 01:55 PM.
Reply With Quote Reply With Quote Share on Google+ Apr 19th, 2015, 02:11 PM #2
stum stum is offline
Hyperactive Member

Join Date
Oct 2013
Posts
389
Re: Webbrowser - loading document complete more then one time
Yes, this is indeed an annoying issue.

On the few times that i did use WebBrowser control, I usually verified completion
based on (WebBrowser.ReadyState = READYSTATE_COMPLETE) combined with a 1-time
Boolean,
to avoid invoking same code more than once.

From my experience using the WebBrowser events is unreliable to say the least, to a
point where I actually preferred enabling a timer to check for .ReadyState combined
with a known .LocationURL than to trust the _Change or _DocumentCompleted \
DownloadComplete \ NavigateComplete2 \ etc.. events to fire it up.

Code:
Dim boolAlreadyExecuted As Boolean
Code:
If (WebBrowser.ReadyState = READYSTATE_COMPLETE) And Not (boolAlreadyExecuted)
Then
'...code to run...
boolAlreadyExecuted = True
End If
Reply With Quote Reply With Quote Share on Google+ Apr 19th, 2015, 02:55 PM #3
Max187Boucher Max187Boucher is offline
PowerPoster

Join Date
Aug 2011
Location
B.C., Canada
Posts
2,887
Re: Webbrowser - loading document complete more then one time
Sometimes I use a loop

Code:
Private Sub WaitForBrowser()
Do While WebBrowser1.ReadyState <> READYSTATE_COMPLETE: DoEvents: Loop
Do While WebBrowser1.Busy = True: DoEvents: Loop
End Sub

Private Sub Command1_Click()


Debug.Print Time & " - " & "Navigating to page....."
WebBrowser1.Navigate "www.vbforums.com"
WaitForBrowser
Debug.Print Time & " - " & "DONE! - " & WebBrowser1.LocationURL
End Sub
This "usually" works 95% of the time
Reply With Quote Reply With Quote Share on Google+ Apr 19th, 2015, 03:11 PM #4
jmsrickland jmsrickland is offline
PowerPoster

Join Date
Jan 2008
Posts
11,072
Re: Webbrowser - loading document complete more then one time
Use the BeforeNavigate2 to get your pages first. Cancel them until you get the
one(s) you want then allow those to be loaded.

I don't think this is what you want to use:

If InStr(WebBrowser1.Document.body.outerText, "Install.exe")

but I am not 100% sure in your case as you are looking for something within the
document so maybe it's OK

One way to get the correct page is to put the URL in a variable:

MyURL = "www.somewebsite.com"

then navigate to that:

WebBrowser1.Navigate MyUrl

then see if current URL has same string in it as MyURL


If InStr(URL, MyURL) > 0 Then

In any case you should use BeforeNavigate2


Last edited by jmsrickland; Apr 19th, 2015 at 03:18 PM.

Anything I post is an example only and is not intended to be the only solution, the
total solution nor the final solution to your request nor do I claim that it is. If
you find it useful then it is entirely up to you to make whatever changes necessary
you feel are adequate for your purposes.
Reply With Quote Reply With Quote Share on Google+ Apr 20th, 2015, 10:03 AM #5
sinner0636 sinner0636 is offline
Thread Starter
Addicted Member

sinner0636's Avatar Join Date


Sep 2009
Posts
233
Re: Webbrowser - loading document complete more then one time
Stum the Boolean worked like a charm it works a lot better then relying on web
browser it self thanks for the info and code
******deb hbaer un timepo de intervalo en el timer, talvez 3000
MODULE
Code:
Public StrVersion As Variant
Public boolAlreadyExecuted As Boolean
Public WebBusy As Boolean
Public WebComplete As Boolean
TIMER CODE
Code:
Private Sub Timer2_Timer()
If WebBrowser1.ReadyState <> READYSTATE_COMPLETE Then
WebComplete = True
Else
WebComplete = False
End If

If WebBrowser1.Busy Then
WebBusy = True
Else
WebBusy = False
End If
End Sub
WEBBROWSER DOCUMENT COMPLETE
Code:
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, url As Variant)

If WebComplete And Not WebBusy And Not (boolAlreadyExecuted) Then


If InStr(WebBrowser1.Document.body.outerText, "Install.exe") > 0 Then
Dim NewVersion As String
NewVersion = Trim(NthField(NthField(WebBrowser1.Document.body.outerText,
"Install.exe", 2), "Browser", 2))

If NewVersion = StrVersion Then


MsgBox "No update available you have the latest version!"
boolAlreadyExecuted = True
End If

If NewVersion = StrVersion + 1 Then


Select Case MsgBox("Update available would you like to download now?", vbYesNo)
Case vbYes
ShowURL ("MyDownloadLink")
boolAlreadyExecuted = True
Case vbNo
boolAlreadyExecuted = True
End Select
End If
End If
End If
End Sub

You might also like