0

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
braX
  • 11,506
  • 5
  • 20
  • 33
  • No. My case str_example is a "string" not a object. So I cannot use "set". Also, It is working if I run the code line by line using "F8" – Youngseok Seo Mar 05 '20 at 04:58
  • Likely your code is failing because of a timing problem: although the page is "loaded" there is some post-load DOM building going on (maybe via some AJAX-type process) which has not completed before you try to access the element. If you debug then it has time to complete. Try adding a wait in your code to allow time for the page to finish building itself. – Tim Williams Mar 05 '20 at 05:59
  • Thank you. Mr. Williams. It was solved by adding 800ms time delay. – Youngseok Seo Mar 05 '20 at 07:07

0 Answers0