Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

browser asking for 'with - end with'

Status
Not open for further replies.

peterd51

Technical User
Feb 22, 2005
109
GB
Hi,

this is odd in that I've been using a simple browser code for years but now it suddenly started telling me I have to use 'with'...and TBH I wouldn't know where to start with that.

<code>
Private Sub Form_Load()

<Set up>

do
Go1 = "no"

Timer1.Interval = 3000 '3 seconds
Timer1.Enabled = True

WebBrowser1.Navigate2 (url)

While Go1 = "no"
DoEvents
Wend

loop, etc

end sub



Private Sub Timer1_Timer()

Timer1.Enabled = False
DoEvents

'it errors here telling me to use 'with - end with'
xfer3 = WebBrowser1.Document.body.parentNode.innerText

If InStr(xfer3, xfer2) Then 'check for specific name
If InStr(xfer3, Date1) Then 'check start date
If InStr(xfer3, Date2) Then 'check end date
'save inner text
xfer3 = Location & "inner\" & xfer2 & " 1 InnerText.txt"
Open xfer3 For Output As #2
Print #2, WebBrowser1.Document.body.parentNode.innerText
Close #2

List1.AddItem xfer2 & " page saved "
Go1 = "yes"
DoEvents
Exit Sub 'page done, no need to re-enable the timer
End If
End If
End If

List1.AddItem "Timer"
Timer1.Interval = 1000 'one second
Timer1.Enabled = True
DoEvents

Exit Sub
</code>


the first section gets the url and starts the browser then loops until it gets a 'completed' signal, then gets the next URL.

After three seconds the timer fires and it checks to see if the page has loaded, contains the right 'name' and 'dates' and if so it saves the page.

If not then it resets the timer and fires again at one second intervals.

It now stops at the 'timer sub' line 'xfer3 = browser...innertext'.


Any idea please?

BTW, Document complete doesn't work as it fires half-a-dozen times as the page is loading...

and then fires again as the 'next' page is loading and messes up any counting I can do.

Regards
Peter

 
Seems very odd, but the actual error number and message might reveal more.

Why all these DoEvents() function calls? It helps a lot to use the events this control raises instead. The logic you have there looks pretty weird actually, probably at least in part because of the strange use of DoEvents() and a Timer control.
 
>The logic you have there looks pretty weird

I'd have to agree ...
 
Hi,

I write in Quick basic but it works OK in VB.

'DoEvents' lets the processor interrupt the code. Without it the while go1="no" : wend would never stop, messages wouldn't display in the list box, etc.

Ignore them, they're not important.

The timer works OK, fires as it should, that's not a problem.

I forgot to mention that it gets the first two pages and stops on the third.

Has anyone any idea how to use a 'with' on a 'Webrowser...innertext'?

Regards
Peter

 
Hi,

I've had a chance to run it again at home and the error is:

'run time error 91 object variable or with block variable not set'

I know it's not the xfer3 causing it as I use the three 'xfer' variables as short-term holders or 'transfer' variables in various places.

As I've never used 'with' I guess it's the Webrowser that's causing it so I'll play around with that.

It just about worked waiting for 'document complete' but I had to watch it all the time and kick it occassionally when it locked itself up.

I've tried using various things on the webbrowser.document... but nothing seems to do exactly what I want without causing some other problem.

I was trying the timer and re-checking the downloaded page as a way of getting around the multiple 'document completes' problem.

Regards
Peter
 
I think you'll find that both dilettante and myself are fairly familiar with DoEvents, what it actually does, and the pros and cons of when to use it.

Suffice it to say that avoiding use of DoEvents wherever possible is a good rule of thumb in VB.


Your problem is that you are trying to do stuff with the webbrowser in the form's load event (and thus before the form is shown) - which actually means that the webbrowser control has not actually been instantiated yet - hence the error.

Either add a Form1.Show at the beginning of the Form_Load event, or move your code into a command button, or anything that runs AFTER the form is shown.
 
Hi,

sorry, I thought from your reply that you wasn't aware of the command. I usually find that I can't get my code to do what I want or expect it to do without using doevents.

I found that:

if len(WebBrowser1.Document.body.parentNode.innerText) > 0 then
xfer3, etc
do the test
save
end if

...works OK.

There's a 'show' right at the start of the form_load.

So fortunately I don't have to mess around learning the 'with' command!

Regards
Peter
 
>So fortunately I don't have to mess around learning the 'with' command!

Which is lucky, since the error was nothing to do with the With command.
 
Here's a slightly less laborious version:
Code:
Option Explicit

Private Sub Form_Load()
    '<Set up>

    WebBrowser1.Navigate2 URL
End Sub


Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
   'Check whether object flagging completion is the main webbrowser rather than any contained frames or subframes
   If (pDisp Is WebBrowser1.Object) Then
    'run your code here ...
    ' My example simply prints out the innertext
    Debug.Print WebBrowser1.Document.body.parentNode.innertext
   End If
End Sub
 
Hi,

Thanks, I'll play with that.

Regards
Peter
 
For you to get that error, I would have thought that it means you have an extra End "Something" statement in your code workflow somewhere (maybe in your combination of the timer, webbrowser and do events).
I have noticed VB can give you a misleading error statement. All it can tell that something is missing and can confuse End Withs with End Ifs, Loop-Dos with For-Nexts.
It might be really telling you you have too many End Ifs or not enough Ifs in your work flow.
The reason for the error may be that with all your doevents, a routine is starting before another one ever ends.
 
No, it's to do with the fact that the webbrowser has not finished loading a document. In other words

WebBrowser1.Document

is nothing, but

xfer3 = WebBrowser1.Document.body.parentNode.innerText

is expecting it to be an object. Which it isn't. Hence the error.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top