1

all. I would like to click the button named id='#ICSearch'.Runtime error 424, object required. Since it is an on-click button that loads js. Will that be a problem? I tried different codes but cannot get it. I am new to this please bear with me.

Private Sub IE_Autiomation()
    Dim i As Long
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")

    '1.
    IE.document.getElementById("ICSearch").Click

    '2.
    IE.document.getElementsByClassName("PSPUSHBUTTONTBADD")(0).Click

End Sub

<DIV class='' id='win0divSEARCHBELOW'><br/><br /><a class='PSPUSHBUTTON'   
  id = 'Left' role='presentation'><span style='background-Color: transparent;border:0;'>    
  <input type='button'     
    id='#ICSearch' name='#ICSearch'  class='PSPUSHBUTTONTBADD'      
    value='Add' onclick="javascript:submitAction_win0(document.win0, 
    '#ICSearch');" tabindex='18' alt='Add (Alt+1)' title='Add (Alt+1)'/> 
  </span></a>
</DIV>
Community
  • 1
  • 1
Sai
  • 13
  • 3
  • Is this the only code that you are using - if not, show more. And possibly format the 1 line of HTML a bit better. However it seems that you are missing `IE.navigate` and etc. Check here - https://stackoverflow.com/questions/28153744/use-excel-vba-to-click-on-a-button-in-internet-explorer-when-the-button-has-no – Vityata Feb 14 '18 at 08:57
  • I omitted that part as that was an internal website but I used the navigate twice and it was OK when I was debugging. I saw the posted link before. I was wondering why it returns "object required" error – Sai Feb 14 '18 at 09:02

1 Answers1

1

This is probably the smallest example of clicking of a button in InternetExplorer with VBA:

Option Explicit

Public Sub TestMe()

    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")

    IE.Visible = True
    IE.navigate "https://github.com"

    While IE.Busy
        DoEvents
    Wend

    Dim objLoginLink As Object
    Set objLoginLink = IE.Document.getElementsByClassName("text-bold text-white no-underline")
    objLoginLink(0).Click

End Sub

This is how objLoginLink looks in the observer window:

enter image description here

The trick is that IE.Document.getElementsBySomething returns a collection, thus you can either loop through it or try to click on the first one.

Use Excel VBA to click on a button in Internet Explorer, when the button has no "name" associated

Vityata
  • 42,633
  • 8
  • 55
  • 100
  • I tried this before and it returns "91" error. object variable or with block variable not set vba . I also tried yours, by adding an object variable. It gives the same error though. IE.document.getElementsByClassName("PSPUSHBUTTONTBADD")(0).Click – Sai Feb 14 '18 at 09:46
  • @Sai - then probably you are not getting the correct class. Have you tried my code with copy+paste exactly? Trying to login to GitHub.com? – Vityata Feb 14 '18 at 09:55
  • I have tried your code with Github and it works. I think the problem is from my HTML code. May you see the HTML code I have pasted and point out what exactly the problem of my code is? IE.document.getElementsByClassName("PSPUSHBUTTONTBADD")(0).Click – Sai Feb 14 '18 at 14:09
  • @Sai - are you sure that you navigate correctly to the corresponding site? – Vityata Feb 14 '18 at 14:10
  • 1
    You are right. My code is right. I was on the wrong site. Thx! – Sai Feb 15 '18 at 03:30