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>
2
  • 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 - stackoverflow.com/questions/28153744/… Commented Feb 14, 2018 at 8: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 Commented Feb 14, 2018 at 9:02

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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 - then probably you are not getting the correct class. Have you tried my code with copy+paste exactly? Trying to login to GitHub.com?
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 - are you sure that you navigate correctly to the corresponding site?
You are right. My code is right. I was on the wrong site. Thx!

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.