0

I'm looking for a code to put values from an Excel to a Webpage.

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

  IE.navigate "http://xxxxxxxx/"
  IE.Visible = True
  While IE.Busy
    DoEvents  
  Wend
  IE.document.getElementById("xxxxx").Value = "xxxx"
End Sub

The error comes in on IE.document.getElementById("xxxxx").Value = "xxxx line and it says Method 'Document' of object 'IWebBrowser 2' failed.

I'm looking for suggstions to solve this question: I made a lot of research and nothing works :/

Thanks in advance :)

1 Answers1

0

Ana, Here is a working example. You can easily customized this little bit of code to work for you. One time you must be aware of is that I added a reference to my project under TOOLS -> Preferences -> Microsoft Internet Controls. Also another word of caution is on this line Set Elements on this line, if you are looking for something that does not exists you will end up with an error. This working example I put together goes to stack overflow and finds the elements below footer-menu. Let me know if something does not make sense.

Sub TryInternetExplorer()
 'Reference to system32\shdocvw.dll  'Microsoft Internet Controls
Dim IEApp As InternetExplorer
Set IEApp = New InternetExplorer
IEApp.Visible = True
IEApp.Navigate "https://iam.pearson.com/auth/XUI/#login/"
While IEApp.ReadyState <> READYSTATE_COMPLETE And IEApp.ReadyState <> READYSTATE_LOADED
    DoEvents
Wend

'wait for the form to load
Do
Set form = IEApp.Document.getElementsByTagName("FORM")(0) '1st table
DoEvents
Loop While form Is Nothing

 IEApp.Document.getElementsByName("callback_0")(0).Value = "Miguel"
 IEApp.Document.getElementsByName("callback_1")(0).Value = "pass"
 IEApp.Document.getElementsByName("callback_2")(0).Click


Set Document = Nothing
Set IEApp = Nothing


 End Sub
Miguel
  • 2,019
  • 4
  • 29
  • 53
  • Miguel, when I copy and paste your code and run it, I have this error: 424: object required :( – Ana Catarina Sabino Mar 29 '17 at 15:24
  • Anna did you add the reference as I told you above? – Miguel Mar 29 '17 at 15:39
  • Set Elements = Document.getElementById("footer- menu").getElementsByTagName("div") – Ana Catarina Sabino Mar 29 '17 at 16:12
  • Thanks in advance for all your help (: – Ana Catarina Sabino Mar 29 '17 at 16:12
  • @ Ana try again there was a space between footer and menu that I most have inserted while copy and paste. I apologize. Make sure that your immediate window is open so you can see the results. – Miguel Mar 29 '17 at 16:40
  • now your code works :) thanks. but when I try to change and use to my website I get the same error! – Ana Catarina Sabino Mar 29 '17 at 17:50
  • The html code for box that I want to fill up is : so i need to use the line like this right? Set elements = document.getElementById("input_20").getElementsByTagName("input") – Ana Catarina Sabino Mar 29 '17 at 17:51
  • Ana what is your website I can't help you working in the blind... and please post that html with code tags in your initial post. – Miguel Mar 29 '17 at 18:07
  • @Ana, I added a comment in code. If you are targeting an input box like the one you suggested you would need to do this instead `IEApp.Document.getElementsByName("q20_yourEmail")(0).Value = "hello"` – Miguel Mar 29 '17 at 18:34
  • Miguel, I can't give you the website because ti's confidencial. But I applied your new code and now I get a different error: code 91 - object variable or with block variable not set. Thanks so much for your time – Ana Catarina Sabino Mar 29 '17 at 18:53
  • https://iam.pearson.com/auth/XUI/#login/ you can use this site as example. I need to fill the user name box and password and then click login :) thanks again – Ana Catarina Sabino Mar 29 '17 at 19:02
  • @Anna, I changed the code to an example of navigating to google and then putting my name on the google search box. That is equivalent to what you are trying to do. Please try it and replace the `getElementsByName` with the name of your `input` that should work barring that your webpage has the input box in the page you are calling. – Miguel Mar 29 '17 at 19:02
  • it can be a problem if my form is inside another form? – Ana Catarina Sabino Mar 29 '17 at 19:19
  • i'm asking because when I use your code for another pages, it works ;/ – Ana Catarina Sabino Mar 29 '17 at 19:22
  • @Anna, you gave me an example and that's what I used to give you an answer. Yes it matters, because you are targeting individual nodes in a webpage,every web page is different and different things are named differently in other pages. Therefore your 'FORM(0)' might be `FORM(01)` in your code etc. – Miguel Mar 29 '17 at 19:25
  • Thanks for your explanation Miguel. when I run the new code I have an error on this line Set form = IEApp.Document.getElementsByTagName("FORM")(0) '1st table compile error: expected function or variable :( – Ana Catarina Sabino Mar 29 '17 at 19:40