title: Check with VBA if an element exists on the page
body: <p>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.</p>
  
  <p>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 </p>
  
  <pre><code>text = document.getelementbyID("result_0").innertext
  </code></pre>
  
  <p>with the error "Object variable or With block variable not set".</p>
  
  <p>How do I check if the element exists before proceeding with the rest of the code?</p>
  
  <p>Thanks!</p>
  
  <p>Sam</p>
  
  <pre><code>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&amp;field-keywords=" &amp; 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") &lt;&gt; 0 Then pos = 1
  
  If pos &lt;&gt; 0 Then Cells(rowe, 4) = "Y" Else Cells(rowe, 4) = "N"
  
  rowe = rowe + 1
  
  Wend
  
  Set ie = Nothing
  
  End Sub
  </code></pre>