drognisep
drognisep

Reputation: 609

How to trigger a specific web page event with a vba macro

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.

What I'm trying to accomplish is to duplicate the manual use of this form - specifically entering the zip code and pressing TAB - with VBA.

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

' 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 <> 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 <> url Then
        curIE.navigate url
      End If

      Set getSite = curIE
    End If
End Function

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

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}"

I've tried just setting the Value property of that element and using oShell.SendKeys 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.

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!

EDIT: Here's the source for the ZipCode field that I'm trying to interact with:

<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">

And I don't want to clutter up this post with the whole source, but it can be found here.

Upvotes: 2

Views: 1604

Answers (1)

QHarr
QHarr

Reputation: 84465

It would help if you showed more HTML from the site and explained why TAB was necessary? If you are selecting another field then reference that element directly.

For the element you are showing, it has an id so use it:

page.getElementById("ZipCode").innerText = "myValue"

There is no javascript to trigger on that element.

Upvotes: 1

Related Questions