7

I am trying to input a value into a from on an internet explorer page using VBA. I have seen similar posts but can't seem to pinpoint my problem. I am somewhat fluent with VBA but HTML is new to me. The ID attributes change on the webpage I am accessing every time you load the page. Here is a shot of the HTML element I am trying to access:

<input type="text" size="20" style="text-align: right; width: 261px;" autocomplete="off"    id="ext-comp-1067" name="user_numbers.1.number_1" class="x-form-text x-form-field x-form- num-field" title="">

The code I am currently using is here:

    Sub OpenWebPage()

    Dim ObjCollection As Object
   Dim i As Long
Dim objElement As Object
Dim doc As Object
Dim form As Object



    Dim ie As Object
    Set ie = CreateObject("INTERNETEXPLORER.APPLICATION")
    ie.Navigate "http://11.182.123.21/#!/view/dashboards/dashboard-1"
    ie.Visible = True
    While ie.Busy
        DoEvents
   Wend


    Application.Wait Now + TimeValue("00:00:10")

 Set ObjCollection = ie.Document.getElementsByName("user_numbers.1.number_1").Value = "123"

End sub

The name "user_number.1.number_1" I think is the only element in the html that I can use to find this input box, the ID changes every time you open the webpage. How do I got about entering a value in this field?

Thank you!

7
  • Does the ID also change id="ext-comp-1067"? Commented Dec 16, 2013 at 13:30
  • Yes, it changes whenever you close the webpage and reload it. The name does not change though that is why I am trying to use it. Commented Dec 16, 2013 at 13:38
  • you have an extra "=" sign. Commented Dec 16, 2013 at 13:39
  • I tried the exact same code and it works for me. BTW which Internet Explorer version is installed on your machine? Commented Dec 16, 2013 at 15:58
  • @PradeepKumar I am using IE 8 Commented Dec 16, 2013 at 16:03

2 Answers 2

5

As sid mentioned, you have an extra "=" sign.

Replace

Set ObjCollection = ie.Document.getElementsByName("user_numbers.1.number_1").Value = "123"

with

ie.Document.getElementsByName("user_numbers.1.number_1")(0).Value = "123"

This is just off my head and not tested code. If this doesn't work replace (0) in the above code with (1).

If there is no other element on the page with the same name then this will work. Otherwise replace (0) in the above statement with appropriate ordinal position.

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

3 Comments

Yes sorry, I forgot to take out the extra "=" sign, that was part of something different I was trying. @pradeep Kumar, I tried as you mentioned above with still no luck. I am getting "runtime error 91, Object variable or With block variable not set". And thank you both for your Input!
I am trying the same code above and am still coming up with the same error. "Object Variable or with block variable not set".
It could be possible that the html tag you are looking at is not in the intenet explorer main window, but inside another frame or iframe. Is that the case?
0

How about finding the ID everytime like this

my_var = ie.doc.body.innerhtml

pos_1 = instr(1, my_var, "user_numbers.1.number_1", vbTextCompare)
pos_2 = InStrRev(my_var, "id", pos_1, vbTextCompare)
pos_3 = instr(pos_2, my_var, "name", vbTextCompare)

my_id = mid(my_var, 3+pos_2, (-1+pos_3) - (3+pos_2)

now try

ie.Document.getElementById(my_id).Value = "123"

I can't get access to your url, so you might have to tweak the numbers added and subtracted in the "mid" function

16 Comments

Ron, I am afraid this is a bit over my head. I am not sure how to go about using the code you posted. I see what you are trying to accomplish though. How would I define all the new variables introduced? Also, any ideas on why this code doesn't work? The name is unique and I cannot write into the input box. 'Code' ie.Document.getElementsByName("user_numbers.1.number_1")(0).Value = "123" 'Code'
Also, I am new here and cannot figure out how to post the code nicely in separate boxes when adding a comment. Any help there would also be great. Thanks!
No need to define the new variables. Just try putting my code after your "Application.Wait" line. Step through the code using F8 and see if you get the desired ID. "InStr" finds the numeric position of the first character in the search term (like "ID") in my_var (which in this case contains the source code behind the web page). InStrRev does the same in reverse (see Excel VBA help for correct syntax and a better explanation). So we're just using the pos_1, _2 and _3 to locate the position of "ID" in the source code. Next we use "mid" to extract the actual ID from the source code.
at pos_2 I get an "invalid procedure or argument" error. I am playing around with the inStrRev function, but nothing has been a success.
did you get a non-zero value for pos_1?
|

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.