Hello every one,
I have a very strange problem with my code. I in my 1st Excel sheet a list of links and a function that loops through those links and sends them to another function that lists the HTML content on my second sheet.
function sheet 1:
Sub Tester()
Dim I, Col As Integer
Dim url As String
Col = 1
ThisWorkbook.Sheets("Sheet1").Activate
For I = 1 To 3
url = Range("A" & Col).Text
Call Sheet2.GetSourceCode(url, Col)
Col = Col + 1
Next
End Sub
function sheet 2:
Sub GetSourceCode(sURL As String, myCol As Integer)
Dim Browser As InternetExplorer
Dim Document As HTMLDocument
Dim Elements As IHTMLElementCollection
Dim Element As IHTMLElement
Dim myRow As Integer
ThisWorkbook.Sheets("Sheet2").Activate
Set Browser = New InternetExplorer
Browser.navigate sURL
Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
DoEvents
Loop
Set Document = Browser.Document
Set Elements = Document.getElementById("content").all
myRow = 1
For Each Element In Elements
If (Element.innerText <> "") Then
Sheet2.Cells(myRow, myCol).Value = Element.innerText
myRow = myRow + 1
End If
Next Element
Browser.Quit
Set Browser = Nothing
Set Document = Nothing
Set Elements = Nothing
Set Element = Nothing
End Sub
Now to my specific problem.
When I run the code I have a Run-time error '91' - Object variable or With block not set error which appears randomly and I don't know where it comes from. But what is really strange is that if y set a Breakpoint and run the whole program step by step no Run-Time error appears and all goes well.
Does anyone have an idea why? Thank you in advance.
Bookmarks