Link to home
Start Free TrialLog in
Avatar of ShawnCorbett
ShawnCorbett

asked on

Webbrowser Stopping process on Error Message

This is a repeat question that i have asked already, however i closed it out too soon. So heres another try at it.

I have made a webbrowser that basicly goes out to the web every half hour to download data from a webpage.
Everything works great, however once and a while i get an error from the page saying "Lost SQL connection during query". When this happens my process halts and waits for a user to walk up and hit ok on the error. I have since added the webbrowser1.silent = true and i get less dialog errors. However this paticular error is not silenced using the previous.

I have also tried refrencing the internet explorer object itself using only code (no control) which the process keeps flowing when this dialog comes up (being that IE is independant of my VB, it just launches a new window on the next cycle) however i cannot get a document_complete message back when the page loads so that method isn't really useful either. (I may just be missing something altogether)
I would prefer to use the webbrowser control if i can find a way to silence or trap that error when it comes up so that my process keeps flowing. (As a temporary workaround i have vb move the mouse to the middle of the screen and send a click every half hour or so to click ok on the button) This is of course a complete hack job and i would like to handle it in a cleaner manner. I really wanted to post a screen shot of the error in the hopes that someone would relate to it beter. If anyone has any good ideas i would greatly appreciate it. (Meanwhile i fight with the site admin to get the sql errors stopped at the root, but this is a slow and painful process. Especially so close to year end) Thanks again.     -Shawn
Avatar of Shiju S
Shiju S
Flag of United States of America image

Hi
Can u post the code u r using, and show the position where u get that error

;-)
Shiju
Avatar of ShawnCorbett
ShawnCorbett

ASKER

I can post the code but it won't do much good. The error is not coming from within my program. If you brought up IE and went to the webpage the same error box would come up if your timing was right. Its basicly a slip between the sql server and the webpage. However when my process is running it launches the webpage and becuase the sql error is coming from the browser I created, the program halts there. here is the code i am using if it helps. Its very basic.

Private Sub Form_Load()
  WebBrowser1.Silent = True
End Sub

Private Sub Timer1_Timer() 'every half hour
    WebBrowser1.Offline = False
    WebBrowser1.Navigate "www.blah.com"
End Sub

Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
     Select Case URL
       Case "www.blah.com/index.php"
           login
       Case "www.blah.com/home.php"
           copydata
     End Select
End Sub

Private Sub login()
 On Error GoTo alreadyin
   WebBrowser1.Document.Forms(0).Item("Uname").Value = "ME"
   WebBrowser1.Document.Forms(0).Item("Email").Value = "MYEMAIL"
   WebBrowser1.Document.Forms(0).Item("Password").Value = "MYPASSWORD"
   WebBrowser1.Document.Forms(0).Item("submit").Click
 Exit Sub
alreadyin:
  If Err.Number = 91 Then WebBrowser1.Navigate "www.blah.com/home.php" else Resume Next
End Sub
hi

 >>Private Sub Timer1_Timer() 'every half hour

What do u mean by every half hour, if u r using Timer control , its interval is in milliseconds. that is
For example, a value of 10,000 milliseconds equals 10 seconds. The maximum, 65,535 milliseconds, is equivalent to just over 1 minute.

This may be the reason of hanging
after few seconds u r aging executing the statement
>>WebBrowser1.Navigate "www.blah.com"


;-)
Shiju
Thats just the basic underlining routine. I bascialy use the timer1.tag to increment every minute (interval 60000) adding one until the timer1.tag = 30. This is the equivalant of half hour. And works fine. However as i have said the problem isn't coming from vb, it is coming from the webpage. The Webpage is having problems with accessing the SQL database. When this happens a Msgbox comes up from IE saying "Lost SQL connection during query". This is becuase the Domain and the SQL database are not currently in synch. I want to be able to close this window when it comes up.
SO i am looking for a way to detect that the popup error came up and once it does be able to close it. becuase it basicly puts my webbrowser1 into a stopped mode waiting for someone to walk up and hit ok on the msgbox. This error is NOT comming from within VB it is coming from the internet exploder object and webbrowser1.silent = true does NOT silence this paticular error box. Is there any other way to silence or detect IE (script errors/page errors)?

-shawn
Hi, ShawnCorbett.

Would it be possible for you to have your Timer regularly scan for the presence of the pop-up error window, and just close it using the APIs I've listed below?  To do this:

1)  Add the following code to your form's General Declarations section:

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

     Private Const SW_SHOW = 5
     Const WM_CLOSE = &H10


2)  Next, initialize the following variables in your form's Form Load event:

    Dim winHwnd As Long
    Dim RetVal As Long
    Dim strPopupName As String


3)  Then, in your Timer routine, add:

     'Private Sub Timer1_Timer()
   
          strPopupName = "Lost SQL connection during query"
          winHwnd = FindWindow(vbNullString, a)
          'Msgbox winHwnd
         
          If winHwnd <> 0 Then
              PostMessage winHwnd, WM_CLOSE, 0&, 0&
          End If

     'End Sub

Note:  I've got this set up with strPopupName = "Lost SQL connection during query".  You may need to alter the text in quotes.  It needs to exactly match the title of your error message's pop-up box.  To see if the handle of the pop-up is being found, you can enable the "Msgbox WinHwnd" above--if it displays "0", then title needs to be adjusted for the "strPopupName = ..." statement.
edwardiii, Thanks for the post I am trying it now. however i have one question. strPopupName is set to "Lost SQL connection during query" however should the FindWindow statement be different? Where do i search for the string. I am guessing maybe it should be like below?

          strPopupName = "Lost SQL connection during query"
          winHwnd = FindWindow(vbNullString, strPopupName) instead of 'a'

Also the string strPopupName should be looking for the title not the text in the message right? Becuase the popup i get simply says "Internet Explorer" and the text in the box would be "Lost SQL connection during query". So i should be looking for "Internet Explorer" Right? Also Does this API look at all currently open windows? or just popup windows? Thanks again for your help!

-Shawn

ASKER CERTIFIED SOLUTION
Avatar of edwardiii
edwardiii

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
edwardiii,  That solution seems to work awesome. The only downside i see is that any window that comes up with "Microsoft Internet Exploder" will be closed. But i use Firefox for a browser anyways So its not anything that i am worried about. Thanks again for your help!

-Shawn
Glad I could help:)