Run-Time Error '91' during web automation/scrape

jerbaldw

Board Regular
Joined
Sep 6, 2012
Messages
89
I keep getting a Object variable or With block variable not set for some code to scrape a internal webpage for information. I cannot figure out how to fix this. As I add more elements to scrape, eventually the last one will error our like this. Or if I move to a new page to scrape, the first object will error.. Thanks for any help.

This is the line that errors (even while stepping through):
Code:
[COLOR=#ff0000]
TXT2 = T2.getAttribute("innerText")
[/COLOR]

This is my code:

Code:
 Private Sub WebScrape
'
[COLOR=#0000ff]Dim [/COLOR]appIE [COLOR=#0000ff]As [/COLOR]SHDocVw.InternetExplorer
[COLOR=#0000ff]Set [/COLOR]appIE = [COLOR=#0000ff]New [/COLOR]SHDocVw.InternetExplorer
'
[COLOR=#0000ff]Dim [/COLOR]PLAN, btnInput, ElementCol [COLOR=#0000ff]As Object
Dim [/COLOR]S1, T1, S2, T2[COLOR=#0000ff] As Object
Dim [/COLOR]VAL1$, TXT1$, VAL2$, TXT2$
'
'
'
[COLOR=#006400]'OPEN INTERNET EXPLORER, GO TO WEBPAGE
[/COLOR]appIE.Visible = [COLOR=#0000ff]True [/COLOR][COLOR=#2f4f4f]'Debug=True
[/COLOR]appIE.navigate "WEBPAGE HERE"
'
[COLOR=#0000ff]Do While [/COLOR]appIE.Busy: DoEvents:  [COLOR=#0000ff]Loop[/COLOR]
    [COLOR=#0000ff]Do While [/COLOR]appIE.readyState <> 4: DoEvents: [COLOR=#0000ff]Loop[/COLOR]
'
'
[COLOR=#2f4f4f]' ENTER DATA INTO TEXT BOX
[/COLOR][COLOR=#0000ff]Set [/COLOR]PLAN = appIE.document.getElementsByName("number")
'
[COLOR=#0000ff]If Not [/COLOR]PLAN Is [COLOR=#0000ff]Nothing Then
[/COLOR]   PLAN(0).Value = [A1]
[COLOR=#0000ff]End If
[/COLOR]'
'
[COLOR=#2f4f4f]' CLICK 'SUBMIT' BUTTON
[/COLOR][COLOR=#0000ff]Set [/COLOR]ElementCol = appIE.document.forms(0)
'
[COLOR=#0000ff]For Each [/COLOR]btnInput [COLOR=#0000ff]In [/COLOR]ElementCol
    [COLOR=#0000ff]If [/COLOR]btnInput.Value = "Search" [COLOR=#0000ff]Then[/COLOR]
        btnInput.Click
[COLOR=#0000ff]       Exit For
    End If
Next [/COLOR]btnInput
'
'
[COLOR=#0000ff]Do While [/COLOR]appIE.Busy: DoEvents:  [COLOR=#0000ff]Loop[/COLOR]
    [COLOR=#0000ff]Do While [/COLOR]appIE.readyState <> 4: DoEvents: [COLOR=#0000ff]Loop[/COLOR]
'
'
[COLOR=#2f4f4f]'SCRAPE PAGE
[/COLOR][COLOR=#0000ff]Set [/COLOR]S1 = appIE.document.getElementsByTagName("td").Item(21) [COLOR=#2f4f4f]'TEXT[/COLOR]
VAL1 = S1.getAttribute("innerText")
[COLOR=#0000ff]If [/COLOR]VAL1 <> "" [COLOR=#0000ff]Then [/COLOR]Sheets("Sheet1").[B2] = VAL1
'
[COLOR=#0000ff]Set [/COLOR]T1 = appIE.document.getElementsByTagName("th").Item(18) [COLOR=#2f4f4f]'TITLE[/COLOR]
TXT1 = T1.getAttribute("innerText")
[COLOR=#0000ff]If [/COLOR]TXT1 <> "" [COLOR=#0000ff]Then [/COLOR]Sheets("Sheet1").[A2] = TXT1
'
'
[COLOR=#0000ff]Set [/COLOR]S2 = appIE.document.getElementsByTagName("td").Item(34) [COLOR=#2f4f4f]'TEXT[/COLOR]
VAL2 = S2.getAttribute("innerText")
[COLOR=#0000ff]If [/COLOR]VAL2 <> "" [COLOR=#0000ff]Then [/COLOR]Sheets("Sheet1").[B3] = VAL2
'
[COLOR=#0000ff]Set [/COLOR]T2 = appIE.document.getElementsByTagName("th").Item(31) [COLOR=#2f4f4f]'TITLE
[/COLOR]TXT2 = T2.getAttribute("innerText")
[COLOR=#0000ff]If [/COLOR]TXT2 <> "" [COLOR=#0000ff]Then [/COLOR]Sheets("Sheet1").[A3] = TXT2
'
End Sub
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I keep getting a Object variable or With block variable not set for some code to scrape a internal webpage for information.
Code:
[COLOR=#ff0000]
TXT2 = T2.getAttribute("innerText")
[/COLOR]
It means T2 is Nothing, which means the line preceding it didn't find the "th" tag with index 31. So you might need:
Rich (BB code):
If Not T2 Is Nothing Then
   TXT2 = T2.getAttribute("innerText")
End If
 
Upvote 0

Forum statistics

Threads
1,203,068
Messages
6,053,346
Members
444,654
Latest member
Rich Cohen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top