• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

How can I get rid of the following errors?

shahin

Active Member
My script is supposed to parse some information from a webpage and it is doing fine to some extent. However, it throws an error "Object variable or with----" when the item I'm looking for is not available in that page.

Few days back while dealing with a similar problem I got stuck. Narayan gave me a solution then to get rid of that. For some weird reason the same solution is not working here. Perhaps there is any trick of it's usage I don't understand.

Code:
Set post = html.getElementsByClassName("data")(2).getElementsByTagName("th")
If Not post Is Nothing Then  '''tried with "If Not post(0) Is Nothing" as well
    cel.Offset(0, 2) = post(0).innerText
End If

Btw, I've searched for few items using inputbox search in a webpage. When I hit the search button and any search is not a match, it leads me to another page. The above error I'm having when my browser leads me to another page.

I can get rid of that If I wish to do something like below but I wanna be stick to any conditional statement to fix the error I'm having.
Code:
On Error Resume Next
Set post = html.getElementsByClassName("data")(2).getElementsByTagName("th")
cel.Offset(0, 2) = post(0).innerText
On Error GoTo 0
 
Last edited:
Hi Shahin ,

By now you should be an expert in debugging !

What do the following display when you type them out in the Immediate window ?

?post is Nothing

?post(0).innerText

Narayan
 
Hi Narayan!! Thanks for your kind reply. I've attached a file with the executable macro. The portion I would like to get refined is commented out.
 

Attachments

  • Experi.xlsm
    29.5 KB · Views: 6
Hi ,

When I execute the procedure , execution halts on this line :
Code:
Set post = html.getElementsByClassName("data")(2).getElementsByTagName("th")
with the error you mentioned.

Now , I displayed and got the following results :

?typename(html) .......................................................... HTMLDocument

?typename(html.getElementsByClassName("data")) .......... DispHTMLElementCollection

?typename(html.getElementsByClassName("data")(2)) ....... Nothing

At this stage , if what is displayed is Nothing , then any further qualification is going to result in an error ; it is exactly like if :

rng is Nothing , then the following will generate an error :

?rng.Value

Thus , the check should not be :

If Not post is Nothing

It should be :

if Not html.getElementsByClassName("data")(2) is Nothing

Or alternatively , you could change your code as follows :
Code:
        Set post = html.getElementsByClassName("data")(2)
        If Not post Is Nothing Then
          cel.Offset(0, 2) = post.getElementsByTagName("th")(0).innerText
        End If
Narayan
 
Awesome!! It is running perfectly now. Thanks a lot Narayan for your wonderful solution.
 
Your provided demo is working like magic. However, I just tried with your first guideline using the below way:

Code:
If Not html.getElementsByClassName("data")(2) Is Nothing Then
Set post = html.getElementsByClassName("data")(2).getElementsByTagName("th")
   cel.Offset(0, 2) = post(0).innerText
End If

And, it is working flawlessly as well. I just pasted the portion to reassure that I could understand your instruction properly. Ain't the way you suggested me to do? Once again: It's working errorlessly. Thanks again Narayan.
 
Back
Top