0

I am quite new to VBA/html, trying to automate filling a form, but cannot even call it ... I searched the web for weeks tried many different code combinations, but none got me there.

The HTML code/element looks like this:

div title:"TextText" class:"text-truncate mb-2" id="abc_sidebar_startmenuitem" data-type="record" data-appid="82" data url="index.cfm?Xxx=&Yyy=">

i class="fa icon-app-82 mr-1 fa-fw">/i>

span class="clickable" id="ext-eng123">Text/span>
/div>

Problem is that class="clickable" is 30th appearance of clickable on the page, id="ext-eng123" is built from the text ext-eng and 3 variable unknown digits, always different.

Example of VBA code used:

Sub GetClick()
    Dim ie As Object
    Set ie = CreateObject("internetexplorer.application")
    With ie
        .Visible = True
        .navigate "https://company.application.com/home/index.cfm?Tab=home"
        Do While .Busy
            DoEvents
        Loop
        Do While .readyState <> 4
            DoEvents
        Loop
    End With
    Dim objIE As Object
    objIE = document.getElementByClassName("clickable")(29)
    objIE.Click
End Sub

I tried over 10+ different code samples, including calling frame number, none worked, I am stuck.

8
  • "weeks"? Please share some more of what you've tried, and the results. "None Worked" isn't much help. Errors? Where/when/why? What's the data look like, and what do you need it to look like? Commented Jun 8, 2018 at 1:07
  • Get the element with id "abc_sidebar_startmenuitem" then call getElementsByClassName("clickable") on that element. Commented Jun 8, 2018 at 5:26
  • Many thanks for replies. @Tim: "abc_sidebar_startmenuitem" and getElementsByClassName("clickable") appears on the page in 8 different combinations. it is 8 spans with id "ext-eng-+++" where +++ are 3 numbers, unique for each span, but whenever the page is click they are different. @ ashleedawg I will post som,e examples with err message below. weeks, but not full-time, couple hours every other day :) Commented Jun 8, 2018 at 19:15
  • id is supposed to be unique on any given page, so that sounds like it's a bit broken. Commented Jun 8, 2018 at 19:21
  • Some more examples of codes that didn't work Commented Jun 8, 2018 at 20:00

1 Answer 1

2

Try the following code. It is hard to give any solution without playing with that site. They are always hypothetical.

Sub GetClick()
    Dim IE As New InternetExplorer, Html As HTMLDocument

    With IE
        .Visible = True
        .Navigate "https://company.application.com/home/index.cfm?Tab=home"
        While .Busy = True Or .ReadyState < 4: DoEvents: Wend
        Set Html = .Document
    End With

    ''If the problem still persists, make sure to put here some delay

    Html.querySelector(".clickable[id^='ext-eng']").Click
End Sub

Another approach might be something like (if the word "Text" didn't appear anywhere with the same class name):

For Each elem In Html.getElementsByClassName("clickable")
    If InStr(elem.innerText, "Text") > 0 Then elem.Click: Exit For
Next elem

Reference to add:

Microsoft Internet Controls
Microsoft HTML Object Library
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you SIM, but getting Run-time err 91. Obj or var not set. the "clickable" with "ext-eng" repeats 8 times on the page, mine is 2nd.
What about this Html.querySelector("#abc_sidebar_startmenuitem span.clickable").Click. Tim Williams has already suggested this. You have given us a very minor portion of html elements to provide you with a solution let alone they are badly formatted, i meant no close tags.
Same Sim. I know, my bad, but couldnt figure out how to post it completely given the rules for posting. Added image above if that could help...
Okay, let us try in some different ways. What is it written on that button. Is it the word Text? If it is just check if there is any other button with the same name Text.
If it breaks, there are still few ways to go. Just check if there is anything, i meant anything uniquely used here that has not been used elsewhere such as data-type="record".
|

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.