3

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?

1 Answer 1

1

You can change this on the element property directly.

var objButton = document.getElementById("buttonId");
objButton.onclick = function() { alert("test"); };
Sign up to request clarification or add additional context in comments.

5 Comments

That looks more like JS, not VBA, and I am not able to change the JS directly. I tried objButton.onclick = "function() { alert(""test""); };" based on your answer, but this doesn't work either, unfortunately. It has the same effect as the other methods I've listed. I can see in the watch panel that the attribute value has changed (again, also the type from Variant/Object to Variant/String) and nothing happens when I click.
Call myIEWindow.Document.execScript("document.getElementById('buttonId').onclick = function() {alert('test');};")
This gives me an "Object doesn't support this property or method" error. Looking now for more info RE: execScript...
Playing with the example, I did get this to work: myIEWindow.Document.ParentWindow.execScript "alert('test');" Will try adapting it to my real function code!
This worked, minus an apparent error in my JS (returned 80020101) but that's a different issue now. Thanks for pointing me in the right direction!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.