4

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

jsotola
  • 2,238
  • 1
  • 10
  • 22
mojo3340
  • 534
  • 1
  • 6
  • 27
  • IE11. should i attempt firefox? – mojo3340 Jul 27 '17 at 12:55
  • If you can't share the URL, what `date-picker` is used on the page? Could you be more specific here? – Daniel Dušek Aug 02 '17 at 07:49
  • http://alloyui.com/examples/datepicker/ like this. no input box, just selection of a date – mojo3340 Aug 02 '17 at 10:39
  • IE11 but i am not using any VBscript – mojo3340 Aug 03 '17 at 14:36
  • @user1 yes you are. In the browser its known as VBScript & in excel and other Microsoft applications its known VBA. It can be confusing yes. Here is an example here http://www.rlmueller.net/Programs/IEDisplay.txt that is similar to your code notice it says VBScript at the top. – Lime Aug 03 '17 at 21:04
  • user1, can you specify where you are actually writing your code? @William seems very adamant that you are writing VBScript, which would probably be embedded within a web page or written in a stand-alone file with a VBS extension. You had originally tagged the question VBA, which would mean you are writing code within the Visual Basic editor in Microsoft Excel (or another Office program, but you also added the Excel tag, so I'll assume that far). – AjimOthy Aug 04 '17 at 02:15
  • Hi, i am 100% writing my code within visual basic for applications, within excel. There are no VBS extensions whatsoever. Perhaps i should attempt to rewrite the question to give better context? – mojo3340 Aug 04 '17 at 07:36
  • I am removing the excel tag from this question. – Shrikant Aug 04 '17 at 13:39
  • @user1 you need to give us more code – Lime Aug 04 '17 at 14:17
  • This question is incorrectly tagged in multiple ways I am not going to be the one to edit it because it is unclear and isn't clear. It appears there is both VBA code and VBScript code. – Lime Aug 06 '17 at 22:18
  • @user1, go to the web page with the calendar control, and save the webpage (complete webpage) to your desktop. then open the resulting html file with a web browser. does the offline version retain the dynamic functionality? – jsotola Aug 08 '17 at 03:40
  • is it like this one? https://jqueryui.com/resources/demos/datepicker/default.html# – jsotola Aug 08 '17 at 04:45
  • @jsotola no it is not like the example, that example has a html input element, mine has no input element – mojo3340 Aug 08 '17 at 06:31

1 Answers1

1

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.

S Meaden
  • 8,050
  • 3
  • 34
  • 65
  • VBA is for excel VBScript is for browsers it appears you have created a response that is not how I interpreted the question, but I do believe it is the only valid way to interpret it. – Lime Aug 06 '17 at 22:20
  • there is no onchange element/event in the HTML/Js – mojo3340 Aug 07 '17 at 21:17
  • You need to open up the developer tools of your browser and place breakpoints in the JavaScript to see exactly how it is doing it. I know you are using IE but for investigation using Chrome is better and here is a SO Q&A highlighting https://stackoverflow.com/questions/10213703/how-do-i-view-events-fired-on-an-element-in-chrome-devtools – S Meaden Aug 08 '17 at 08:22