I am trying to change the function call from the onclick event of a button on a form using automation in VBA. Function X is already hard-coded into the webpage, and I do not have access to change that code. (The page is internal to my company, but I am not in that department).
When my scenario fits, I want to replace function X with a new function Y.
I've seem similar questions to this one SO, but it appears most refer to changing an element attribute from within JS code. My problem is that I need to so this in VBA and I'm not sure how to get there.
This site has a good deal of information regarding this process (setAttribute, getAttribute, etc.), including useful info re: caveats when working with one browser vs. another. All my code will be interfacing with IE (what the company provides), so no need to worry about the other browser options.
When trying to change the onclick attribute, the VBE watch panel shows a change in the attribute, but it does not behave as expected. After I have changed the attribute, nothing at all happens when I click (either through my automation of objButton.Click / objButton.onclick, or actually manually clicking the button). I have also tried this on other attributes (i.e. onfocus) with the same results. The link above mentions IE has some difficulty with this, but I'm not overly familiar with JavaScript and I am having trouble following the guide.
What I have tried is the following, all of which have the same result.
Option 1:
Set TEST = myIEWindow.Document.createattribute("onclick")
TEST.nodevalue = "function anonymous() {alert(""test""); };"
objButton.setAttributeNode TEST
Option 2:
objButton.Attributes("onclick").value = "function anonymous() {alert(""test""); };"
Option 3:
objButton.Attributes("onclick").value = "alert(""test"");"
Option 4:
objButton.setAttribute "onclick", "alert(""test"");"
Option 5:
objButton.onclick = "function() { alert(""test""); };"
While trying these different methods out, I did notice that the onclick attribute type in the VBE watch window changes from its original type of Variant/Object to Variant/String after attempting to coerce the button to call the new function, and I feel this has something to do with my problem.
Can someone please explain how (if possible) I can get this to work?