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):
This is my code:
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