How to trigger a specific web page event with a vba macro
<p>Let me start off by saying that I have VERY limited knowledge of web development, so please correct me if I'm wrong about what's going on here. </p>

<p>What I'm trying to accomplish is to duplicate the manual use of <a href="https://www.titleclose.com/Consumer/Welcome" rel="nofollow">this</a> form - specifically entering the zip code and pressing TAB - with VBA.</p>

<p>I'm able to create a new IE instance and navigate to the page with this function:</p>

<pre><code>' Get reference to (maybe) already initialized reference
Public Function getSite(url As String) As InternetExplorer
    Dim oShell As Object

    ' curIE is declared as "Private curIE As InternetExplorer" at module level
    If curIE Is Nothing Then
      Set curIE = New InternetExplorer
      curIE.Visible = True
      curIE.navigate url
      Set oShell = CreateObject("WScript.Shell")

      ' Probably redundant, could I get some clarification on this?
      While (curIE.readyState &lt;&gt; READYSTATE_COMPLETE) Or curIE.Busy ' Wait until it has settled and loaded

      Wend

      ' Maximize the window
      Application.Wait Now + TimeValue("00:00:01")
      oShell.SendKeys "% "
      Application.Wait Now + TimeValue("00:00:01")
      oShell.SendKeys "x"
      Set getSite = curIE
    Else
      If curIE.LocationURL &lt;&gt; url Then
        curIE.navigate url
      End If

      Set getSite = curIE
    End If
End Function
</code></pre>

<p>And I can get the <code>ZipCode</code> element with this snippet, but I can't seem to do the actual work of entering data:</p>

<pre><code>Dim page As HTMLDocument: Set page = getSitePage(curIE.LocationURL) 'Does the work of the GetSite function and gets the document
Dim oShell As Object: Set oShell = CreateObject("WScript.Shell")

Application.Wait Now + TimeValue("00:00:01") 'Seems to help prevent timing issues, is there a better way?

page.getElementsByName("ZipCode").Item(0).Click
oShell.SendKeys "12345{TAB}"
</code></pre>

<p>I've tried just setting the <code>Value</code> property of that element and using <code>oShell.SendKeys</code> to send a tab, but the field doesn't seem to get focus when its value is set in this way. This is the key part right here, because it would be WAY too time consuming to create a look up function for every county in every state, when I know the site can perform the look up itself.</p>

<p>If there's a way to work with the javascript or whatever backing logic is performing the function I'm trying to trigger and someone can show me how, that would be grand. Thanks SO!</p>

<p><strong>EDIT:</strong> Here's the source for the ZipCode field that I'm trying to interact with:</p>

<pre><code>&lt;input name="ZipCode" class="form-control input-lg valid" id="ZipCode" aria-invalid="false" aria-required="true" aria-describedby="ZipCode-error" type="text" placeholder="Zip Code (Required)" value="" data-val-required="Zip Code is required" data-val="true" data-val-regex-pattern="^\d{5}(-\d{4})?$" data-val-regex="Zip Code format is not valid" autocomplete="false"&gt;
</code></pre>

<p>And I don't want to clutter up this post with the whole source, but it can be found <a href="https://www.titleclose.com/Consumer/Welcome" rel="nofollow">here</a>.</p>