P. Mann
P. Mann

Reputation: 59

"Click" a javascript button with VBA

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.

Upvotes: 0

Views: 6540

Answers (1)

Tim Williams
Tim Williams

Reputation: 166126

This should work:

IE.document.getElementById("ctl00_cphMain_btnAddTab").Click

Upvotes: 1

Related Questions