permission denied error when automating explorer

F

festdaddy

Hi,
I have some code that navigates to a website, and places certain links
into a collection. I then try a for each loop through the collection
to navigate to those links. If I just let it run, I get a permission
denied error, but if I pause at the for each line and step through,
everything works okay. Can anybody tell me whats going on? I've done
this kind of thing many times and never had this problem.

Thanks,
Rob
 
J

Joel

Probably all the data isn't available. Database acces or switching pages
takes time and you sometimes have to wait for the page to be ready before you
proceeed.

The code below works when you are naviagating but will not always work

'get web page
IE.Navigate2 URL & Request
Do While IE.readyState <> 4
DoEvents
Loop

Hee is an example where I had to wait for an object to be available before
the code could continue.

but.Select
but.Click

On Error Resume Next ' Defer error handling.
Do
Err.Clear
Set PageNumber = IE.document.getElementById("pageNumber")
Pages = PageNumber.Value
DoEvents
Loop While Err.Number <> 0
 
F

festdaddy

Thanks for responding Joel.

I'm pretty sure that isn't the problem because the collection is
getting filled okay, its when i try to access a member of the
collection and do something with it. I can write each link to a cell,
and access it that way (but that isn't desirable in this case), so it
seems to be some sort of issue with links and collections? I don't
understand why it works if I pause first.
 
J

Joel

At full operating speed you can't tell "when" the object is getting filled.
It may take a half a second to fill the object but the macro is running very
fast and will get to the object before it is filled. When you step through
the code the object gets filled before you require the object.
 
F

festdaddy

Yes, I understand that. I've tried waiting several different ways, and
it doesn't seem to matter. here's the code:

Sub get_data_from_site()

Dim ie As SHDocVw.InternetExplorer
Dim catone As Object
Dim cattwo As Object
Dim catthree As Object
Dim trims As New Collection

nrw = 1
Set ie = New SHDocVw.InternetExplorer

'goto main site for new cars
ie.Navigate "http://www.mysite.com"
While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Wend
Set catone= ie.document

'If I pause here, everything works fine. If I let it run, I get
permission denied, with the If line highlighted. Waiting doesn't seem
to help.
For Each lma In catone.Links
If InStr(1, lma, "condition1") Then
ie.Navigate lma
While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Wend
Set cattwo= ie.document

For Each lmo In cattwo.Links
If InStr(1, lmo, "condition2") Then
ie.Navigate lmo
While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Wend

Set catthree= ie.document

For Each lt In catthree.Links
If InStr(1, lt, "condition3") Then
On Error Resume Next
la = Array(lt, lt.sourceIndex)
trims.Add la, lt
On Error GoTo 0
End If
Next lt

Sheets("test").Activate

For x = 1 To trims.Count
'Cells(nrw, 1) = cat1
'Cells(nrw, 2) = cat2
c = 3

For y = trims(x)(1) To trims(x + 1)(1)
Cells(nrw, c) =
trimsdoc.all.item(y).innerText
c = c + 1
Next y
nrw = nrw + 1
Next x
End If
Next lmo
End If
Next lma
ie.Quit
Set ie = Nothing

End Sub
 
J

Joel

This is how I run my code. Change opening IE and made ready state 4.


Sub get_data_from_site()

'Dim ie As SHDocVw.InternetExplorer
Dim catone As Object
Dim cattwo As Object
Dim catthree As Object
Dim trims As New Collection

nrw = 1
Set ie = CreateObject("InternetExplorer.application")
ie.Application.Visible = True
'goto main site for new cars
ie.Navigate "http://www.mysite.com"
While ie.readyState <> 4
DoEvents
Wend
Set catone = ie.document

'If I pause here, everything works fine. If I let it run, I get
'permission denied, with the If line highlighted. Waiting doesn't seem
'to help.
For Each lma In catone.Links
If InStr(1, lma, "condition1") Then
ie.Navigate lma
While ie.readyState <> 4
DoEvents
Wend
Set cattwo = ie.document

For Each lmo In cattwo.Links
If InStr(1, lmo, "condition2") Then
ie.Navigate lmo
While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Wend

Set catthree = ie.document

For Each lt In catthree.Links
If InStr(1, lt, "condition3") Then
On Error Resume Next
la = Array(lt, lt.sourceIndex)
trims.Add la, lt
On Error GoTo 0
End If
Next lt

Sheets("test").Activate

For x = 1 To trims.Count
'Cells(nrw, 1) = cat1
'Cells(nrw, 2) = cat2
c = 3

For y = trims(x)(1) To trims(x + 1)(1)
Cells(nrw, c) = trimsdoc.all.Item(y).innerText
c = c + 1
Next y
nrw = nrw + 1
Next x
End If
Next lmo
End If
Next lma
ie.Quit
Set ie = Nothing

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top