mojo3340
mojo3340

Reputation: 549

HTML date picker with no input field. use VBA to fire update event

Okay,

A simple problem with a difficult solution (currently)

There is a page with a to and from date picker. The date picker has no html input element but i am able to access it using InnerText. There is no issue with using InnerText to put the to and from dates into

The problem arises as such:

When dates are manually entered, a "form" appears, that updates live as the user chooses the dates, and displays only the data within that date range. When this is done programatically there is no live updating of the "form"

Attempt so far:

Set document = objIE.document

With document
    .getElementsByClassName("controls").Item(0).Focus
    .getElementsByClassName("select-daterange").Item(0).Focus
    .getElementsByClassName("select-datepicker from").Item(0).Focus
    .getElementsByClassName("date-label").Item(0).innerText = "June 1, 2017"
    .getElementsByClassName("date-label").Item(1).innerText = "June 5, 2017"
End With

Appreciate any guidance from the community!

Here is a sample of what the date picker looks like:

No-input Date Picker

Upvotes: 4

Views: 2083

Answers (1)

S Meaden
S Meaden

Reputation: 8270

I had proved it is possible to manually fire a Javascript event from VBA using fireevent.

Some links here

Mozilla Dev Network - EventTarget.fireEvent

Here is my code.

Option Explicit

Sub Test()
    '* Tools->References->Microsoft HTML Object Library
    '* Tools->References->Microsoft Internet Controls

    Dim ie As SHDocVw.InternetExplorerMedium
    Set ie = New SHDocVw.InternetExplorerMedium


    ie.Visible = True
    ie.navigate "n:\TestWebOnChangeManually.html"

    Dim obj As Object

    Stop '* i use this pause to dismiss Allow Block Content footer warning message
    Dim dom As MSHTML.HTMLDocument
    Set dom = ie.document

    Dim textSource As MSHTML.HTMLInputElement
    Set textSource = dom.getElementById("source")

    textSource.innerText = "poked"

    'https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/fireEvent
    '* not sure what to place in second parameter
    textSource.FireEvent "onchange", Nothing


End Sub

and using this html test file

<html>
<head/>

<body>

<input id="source" type="text" onChange="MyChange('source')"></input>

<input id="someOtherControlToFocusSwitchTo" type="text" value="foo"></input>


<script>
    function MyChange(eleId)
    {
        if (eleId)
        {
            var ele = document.getElementById(eleId);
            if (ele) {
                alert(ele.value);
            }
        } else
        {
            alert("MyChange fired with no param");
        }

    }

</script>
</body>
</html>

Elsewhere there seems to be a different approach using dispatchEvent some code copied from SO FireEvent and IE11 though I did not try this. This is VBA code based on some javascript found at SO How can I trigger an onchange event manually? which itself is a duplicate of SO How to trigger event in JavaScript?

' Fire the onChange event...
Dim objEvent
Set objEvent = doc.createEvent("HTMLEvents")
objEvent.initEvent "change", False, True
e.dispatchEvent objEvent

I hope that is enough for you, debugging this stuff can be tricky.

Upvotes: 1

Related Questions