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!

user3107346
  • 71
  • 1
  • 1
  • 3

2 Answers2

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.

Pradeep Kumar
  • 6,836
  • 4
  • 21
  • 47
  • 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! – user3107346 Dec 16 '13 at 14:17
  • I am trying the same code above and am still coming up with the same error. "Object Variable or with block variable not set". – user3107346 Dec 16 '13 at 17:06
  • 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? – Pradeep Kumar Dec 17 '13 at 07:31
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

ron
  • 1,456
  • 3
  • 18
  • 27
  • 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' – user3107346 Dec 16 '13 at 15:58
  • 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! – user3107346 Dec 16 '13 at 16:01
  • 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. – ron Dec 16 '13 at 16:30
  • 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. – user3107346 Dec 16 '13 at 17:09
  • did you get a non-zero value for pos_1? – ron Dec 16 '13 at 17:21
  • I bet my_var is empty, use my_var = ie.document.body.innerhtml – ron Dec 16 '13 at 17:42
  • That is what I used, My_var is not empty. I am trouble shooting now trying to see what is going on. – user3107346 Dec 16 '13 at 17:46
  • for whatever reason I cannot find this string with your code in pos_1 "User_numbers.1.number_1". I tried this with some different text in the HTML and it finds it just fine. Any thoughts? Thank you very much for all of your help by the way. – user3107346 Dec 16 '13 at 18:30
  • Are there other occurrences of "user_numbers"; if you put "user_numbers" in pos_1 does that work? Is "ID" always of the form "ext-comp-ABCD"? If so, search for "ext-comp-" or something similar. I can't get to your website to check it out myself. Sounds like some of the other guys responding to your question were able to get there - wonder why I can't? In any case, this technique has worked for me when I was dealing with a ID that was always changing. – ron Dec 16 '13 at 18:41
  • No one else would be able to reach the site. They must have tried the same code with a different site. It is a local Device on our network, you have to be plugged into the network to access it. It is the only occurrence of the "User_numbers". I have tried searching other text in the HTML and have found it, it is only that text I cannot find, which is probably why my original method did not work as well. As for the ID; that ID for that input is always in that format, I will try that – user3107346 Dec 16 '13 at 18:48
  • I think something more than just being able to find the correct name or ID is going on. Even if I keep the same window open and try to write to the box using the ID given for that instance I still get errors "object variables or with block variables not set". Anyway I know you cannot reach the site so it is a little difficult to trouble shoot so thanks for the help! If anything else strikes you please let me know. – user3107346 Dec 16 '13 at 18:50
  • Do you mean the line ie.Document.getElementById("correct id").Value = "123" doesn't work when you manually insert the correct ID? – ron Dec 16 '13 at 18:55
  • Maybe you're examining the wrong line of HTML. Examine every item on the web page and see which one puts "123" in the target. numb = ie.Document.all.Length - 1 On Error Resume Next For x = 0 To numb ie.document.all.item(x).value = "123" Next Then you can use stuff like a = ie.Document.all.Item(x).innerhtml b = ie.Document.all.Item(x).Name c = ie.Document.all.Item(x).ID to identify the item(x) that successfully put "123" in the box. – ron Dec 16 '13 at 21:42
  • Thanks for the help. I did as you suggested and it turns out I cannot access anything inside the element in the HTML code. Not sure what the even means or why I can't access inside it but that seems to be the problem, as that is where the input box I need to get to is located. – user3107346 Dec 17 '13 at 18:22
  • Aha, an IFrame! To get the HTML inside the iframe, extract the src attribute from the – ron Dec 17 '13 at 20:10
  • Awesome thanks for the help. I will let you know how this turns out! – user3107346 Dec 17 '13 at 20:31