I made following code to get an example sentence from web site, corresponding to a word in the column A. When I run it, I got a runtime error 91 "Run-Time Error '91': Object Variable or With Block Variable Not Set" However, if I press "Debug" after got the error and continue execution by pressing F5, the code fully runs again without an error. Also, if I run the code line by line (with F8), the error is not appeared.
I added references Microsoft HTML Object Library, Microsoft Internet Controls.
I will much appreciate if you can advise why I got error and how to fix it.
Following is the VBA code.
Sub getexample()
Dim ie As InternetExplorer
Dim HT As HTMLDocument
Dim str_URL As String
Dim str_WORD As String
Dim sht As Worksheet
Dim rng As Range
Dim str_example As String
Set sht = ThisWorkbook.Sheets(1)
Set ie = New InternetExplorer
LastRow = sht.Cells(sht.Rows.Count, 1).End(xlUp).Row
For Each rng In sht.Range("A1:A" & LastRow)
str_WORD = rng.Value
'get an example
str_URL = "https://en.dict.naver.com/#/search?query=" & str_WORD
ie.navigate str_URL
Do While ie.readyState <> READYSTATE_COMPLETE
DoEvents
Loop
Set HT = ie.document
str_example = HT.getElementsByClassName("origin is-audible")(1).getElementsByTagName("p")(0).innerText
rng.Offset(0, 1) = str_example
Next
ie.Quit
End Sub