0

endless googling and lots of solutions haven't seemed to help. I keep getting endless errors of different kinds, and just can't find an elegant way of clicking this button unless I get the code to wait for four seconds while I click it manually with my mouse.

It works on other buttons, but I'm working if the problem here is the nested doPostBack and the many '' (substituted from "") in it?

Code below (I've trimmed out irrelevant sections)

VBA Code:

Sub IE_Clicks()

    Dim shellWins As ShellWindows
    Dim IE As InternetExplorer

    Set shellWins = New ShellWindows

    If shellWins.Count > 0 Then
    ' Get IE
    Set IE = shellWins.Item(0)
    IE.Visible = True
    Else
    ' Create IE
    Set IE = New InternetExplorer
    IE.Visible = True
    End If

    IE.Navigate ("[URL]")

Do While (IE.Busy Or IE.READYSTATE <> READYSTATE.READYSTATE_COMPLETE)
    DoEvents
Loop
    Call IE.document.parentWindow.execScript("WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('ctl00$cphMain$btnAddTab', '', true, '', '', false, false))", "JavaScript")

    Set IE = Nothing
 End Sub

HTML Code:

<input type="submit" name="ctl00$cphMain$btnAddTab" value="Add New" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$cphMain$btnAddTab&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ctl00_cphMain_btnAddTab" class="ActionButton">

JavaScript:

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>

Something that would enable me to click this button would be amazing.

4
  • 2
    IE.document.getElementById("ctl00_cphMain_btnAddTab").Click Commented Dec 21, 2016 at 18:21
  • You beauty, thanks. Commented Dec 22, 2016 at 16:47
  • @TimWilliams please add your answer, to allow P.Mann to flag as solved Commented Dec 22, 2016 at 17:45
  • @TimWilliams , actually I have simmilar issue, however my HTML code has no id for href. it is using same javascript. I cannot click the button. link you may see my question in this post. Commented Dec 23, 2020 at 12:56

1 Answer 1

1

This should work:

IE.document.getElementById("ctl00_cphMain_btnAddTab").Click
Sign up to request clarification or add additional context in comments.

Comments

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.