Excel VBA: Error 438 "object doesn't support this property or method
<p>This code is being used to enter a value in web form using Excel VBA:</p>

<pre><code>Sub Call_Site(var1 As String)
Dim oHTML_Element As Object 'IHTMLElement
Dim sUrl As String

sUrl = "https://www.example.com/page"
Set oBrowser = CreateObject("InternetExplorer.Application") 'New InternetExplorer
oBrowser.silent = True
'oBrowser.timeout = 60
oBrowser.navigate sUrl
oBrowser.Visible = False

With oBrowser
    Do While .Busy Or .ReadyState &lt;&gt; 4: Loop
End With

Set HTMLDoc = oBrowser.document.getElementsByClassName("nexcel")(0)

With HTMLDoc
   HTMLDoc.getElementById("var1").Value = var1
End With
Exit Sub
</code></pre>

<p>It happens that line:
       HTMLDoc.getElementById("var1").Value = var1</p>

<p>Is returning the error:</p>

<pre><code>Error 438 "object doesn't support this property or method
</code></pre>

<p>Which only happens sometimes.</p>

<p>I can't understand why the error only happens sometimes (usually when it's critical) and what I can do to prevent it.</p>

<p>Having the browser visilble, it all seems normal.</p>

<p>Nowhere in similar answers I found a solution.</p>