views:

245

answers:

2

This code works great in the IDE but crashes every time on the last line when made into an exe and run on the same machine. Any idea why?

PageFinished = False
Cancelling = False
OKToUnload = False

WebBrowser.Navigate ("https://www.example.com/index.jsp")

Do While PageFinished = False  'set to true in document complete event
    DoEvents
    If Cancelling = True Then
        OKToUnload = True
        GoTo Endline
    End If
Loop
PageFinished = False
WebBrowser.Document.All("UserId").Value = txtNumber.Text 'error here
+1  A: 

Without seeing more of your sample code, I'd venture to guess that this is a timing issue that's "hidden" by the VB IDE. Test WebBrowser.Document.All("UserId") before setting the .Value property. It is probably not available (Nothing) at the time the non-IDE version of the code gets to that point.

"Object Variable or With Block Variable Not Set" is VB's way of telling you about a null reference, and in the line WebBrowser.Document.All("UserId") you have 3 separate objects that could be null.

Jim H.
It was a timing issue as you say, caused by my stupidly reacting to the downloadcomplete rather than the documentcomplete event of the webbrowser. Sorry for the incorrect information.
kjack
+1  A: 

You will need to add msgboxes showing the result of testing which variable are set to NOTHING or write to a text file and run the exe and see what is set to nothing.

It may be as simple as having a wait before the last line. A Wait subroutine looks like this.

Public Sub Wait(T As Double)
    Dim StartTime As Double
    StartTime = Timer
    Do While Abs(Timer - StartTime) < T
    Loop
End Sub

I would try a 1/10 of second and work your way up. i.e Wait .1 If have to wait a second or more then make sure you call DoEvents periodically to keep your application responsive.

The reason for this is that the IDE always uses PCODE so is a touch slower than a EXE complied to EXE. You may want to try compiling to PCODE as well to see if that makes a difference.

RS Conley
Hi RS Your suggestion worked but not consistently, but that was my fault. I screwed up. I had my Pagefinished variable being set to true in the downloadcomplete event of the webbrowser rather than the documentcomplete one. Sorry!
kjack