Click a IE9 Web Page button using VBA

  • Hi good people.


    I am trying to find some code to click this button in the below html code.


    Code
    1. <tr><th class="t13RegionTitle">Find Isolation Lists</th>
    2. <th class="t13RegionButtons" valign="bottom"><input type="button" onclick="javascript:apex.submit(&#x27;RESET&#x27;);" value="Reset Quick Search" class="t13Button" />&nbsp;&nbsp;&nbsp;</th>
    3. </tr>


    I have used code successfully for opening a Intranet URL and clicking on a specific hypertext link on IE9 Web Page, but cannot work out how to write the correct code to click this particular button.


    Any help appreciated.


    Cheers

  • Re: Click a IE9 Web Page button using VBA


    Try:

    Code
    1. Dim resetButton As HTMLInputElement
    2. Set resetButton = IE.document.getElementsByClassName("t13Button")(0)
    3. resetButton.Click

    where IE is the InternetExplorer object loaded with the webpage. The (0) assumes the button is the first element with class name "t13button". If not, try (1), (2), etc.

  • Re: Click a IE9 Web Page button using VBA


    Thanks for the reply.


    I'm getting a compile error with the reset button code added: (This line - Dim resetButton As HTMLInputElement) "user-defined type not defined".


    Without the reset button code everything works fine.


    My excel knowledge is limited. Appreciate your help.


    Snippet of the code:


  • Re: Click a IE9 Web Page button using VBA


    Quote from dejamls;794588

    I'm getting a compile error with the reset button code added: (This line - Dim resetButton As HTMLInputElement) "user-defined type not defined".

    Set (tick) the reference to Microsoft HTML Object Library, via Tools->References in the VBA editor.

  • Re: Click a IE9 Web Page button using VBA


    That got rid of the error, cheers but...


    The button still won't click. There is two instances of the class "t13Button". I've tried try (1), (2), etc.


    No luck.


    I also tried the class "t13RegionButtons"


    Thoughts John.


    Cheers

  • Re: Click a IE9 Web Page button using VBA


    If there are 2 instances of the class "t13button" then it's either (0) or (1). To make sure you are referencing the correct instance, after the Set resetButton line has executed type the following command in the VBA Immediate Window:


    ?resetButton.outerhtml


    and press Enter. (Press Ctrl+G in the VBA editor to display the Immediate Window.) If the code is referencing the correct element then you should see this displayed below the command:


    HTML
    1. <input type="button" onclick="javascript:apex.submit('RESET');" value="Reset Quick Search" class="t13Button" />


    Sometimes the simple Click event isn't enough and dispatchEvent must be used instead. Try this code, which replaces my previous code:


    Code
    1. Dim evtClick As Object
    2. Dim resetButton As HTMLInputElement
    3. Set evtClick = IE.document.createEvent("HTMLEvents")
    4. evtClick.initEvent "click", True, False
    5. Set resetButton = IE.document.getElementsByClassName("t13Button")(0) 'or (1)
    6. resetButton.dispatchEvent evtClick
  • Re: Click a IE9 Web Page button using VBA


    First of all I took any error handling code out of my macro so it didn't exit sub.


    I tried stepping through code and got Runtime error'438': Object doesn't support the property or method.


    With the Ctrl+G action I got Runtime error '91': Object variable or with block variable not set.


    I tried new code too and no luck.


    After code line


    Code
    1. [COLOR=#333333]evtClick.initEvent "click", [/COLOR][COLOR=blue]True[/COLOR][COLOR=#333333], [/COLOR][COLOR=blue]False[/COLOR]


    I got the Runtime error '438'

  • Re: Click a IE9 Web Page button using VBA


    Your post is a little ambiguous so it's hard to understand why this isn't working.


    Quote from dejamls;794598


    I tried stepping through code and got Runtime error'438': Object doesn't support the property or method.

    Which line causes that error?


    Quote


    With the Ctrl+G action I got Runtime error '91': Object variable or with block variable not set.

    Which means resetButton is Nothing (undefined). Are you entering the command after the Set resetButton line has executed? (Step through the code line by line by pressing the F8 key). Did you try (1) instead of (0)?


    You can also use Debug.Print statements to output to the Immediate Window, for example this line placed after the Set resetButton line:

    Code
    1. Debug.Print resetButton.outerHTML


    Do you mean after that line or on that line? If the error occurs on that line it means the initEvent method is not supported which I don't understand if you're using IE9.