2

I have 15,000 products and I need to know if they're X, Y or Z. The code below checks on amazon to see if a product is a type of XYZ.

God help me, it actually works. The only exception is when it searches a product no longer sold by Amazon. Then the element ID that I'm looking for which contains the product description that I'm searching doesn't exist on the page, and the code breaks on line

text = document.getelementbyID("result_0").innertext

with the error "Object variable or With block variable not set".

How do I check if the element exists before proceeding with the rest of the code?

Thanks!

Sam

Sub LetsAutomateIE()

Dim barcode As String
Dim rowe As Integer
Dim document As HTMLDocument
Set ie = CreateObject("InternetExplorer.Application")
Dim Element As HTMLDivElement
Dim text As String
Dim pos As Integer

rowe = 2

While Not IsEmpty(Cells(rowe, 2))

barcode = Cells(rowe, "B").Value

With ie
.Visible = False
.navigate2 "https://www.amazon.co.uk/s/ref=nb_sb_noss_1?url=search-    
alias%3Daps&field-keywords=" & barcode
Do Until ie.readyState = 4
Loop
End With

Set document = ie.document

text = document.getElementById("result_0").innerText

If InStr(text, "X") Or InStr(text, "Y") Or InStr(text,     
"Z") <> 0 Then pos = 1

If pos <> 0 Then Cells(rowe, 4) = "Y" Else Cells(rowe, 4) = "N"

rowe = rowe + 1

Wend

Set ie = Nothing

End Sub
5
  • use error trapping Commented Sep 6, 2016 at 15:08
  • 3
    Try checking to see if just the element returned is an object. Something like set Element = document.getelementbyID("result_0") e.g. don't return the InnerText property, then if isObject(Element) then to check if it returned as an object. Commented Sep 6, 2016 at 15:08
  • Thank you Ryan! What should I dim the element as before it's set? Commented Sep 6, 2016 at 15:36
  • It should be an Object. Commented Sep 6, 2016 at 15:45
  • Thank you for your help! Commented Sep 6, 2016 at 16:50

4 Answers 4

4

Posting an alternative in case top answer is not working for other people:

I was still getting run time error '91' after checking with IsObject()

IsObject() works fine if ObjIE is an InternetExplorer object but I was using an InternetExplorerMedium object and had to validate with this instead since it was returning a nothing object:

If Not objIE.document.getElementById("e164NumberMask") Is Nothing Then
    'do true stuff
Else
    'do false stuff
End If
Sign up to request clarification or add additional context in comments.

Comments

3

Ryan's answer is the correct.

set Element = document.getelementbyID("result_0")

Comments

3

Thanks for the solution. I used as follows,

If IsObject(objIE.document.getElementById("e164NumberMask")) Then
    'do true stuff
Else
    'do false stuff
End If

Comments

0

You can try something like this.

Function objectHandler(objID)

Dim TestObj As Object

On Error GoTo Handler:

Set TestObj = ObjIE.document.getElementById(objID)
objectHandler = True
Exit Function

Handler:
objectHandler = False

End Function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.