2

I am attemting to log into a website using VBA and Internet Explorer. This is causing a Run TIme error 424 "Object Required".

Here is my code. Looking for your expert advise.

Sub Website()

  Dim IE As Object, Doc As Object, UserName As Object, Password As Object, strCode As String

  Set IE = New InternetExplorerMedium
  IE.Visible = True
  IE.Navigate "http://appln.ABC.com/index.epl"

  Do While IE.ReadyState <> 4: DoEvents: Loop

  Set Doc = IE.Document

  Set UserName = Doc.getElementById("login_usr")
  UserName.Value = "ABCDE"

  Set Password = Doc.getElementById("login_password")
  Password.Value = "ABCDE"

  Set btnLogin = Doc.getElementById("Login")
  btnLogin.Click

End Sub
ChrisW
  • 4,970
  • 7
  • 55
  • 92
Stratos7
  • 21
  • 1
  • 2

4 Answers4

1

Did you try the method set forth in this example?

Fill user name and password in a webpage using VBA

I think you need to

Set IE = CreateObject("InternetExplorer.Application")

In your code you aren't actually creating an object, you're just declaring it.

EDIT

Maybe bypass the usage of your Doc object:

Sub Website()

  Dim IE As Object, UserName As Object, Password As Object, strCode As String

  Set IE = New InternetExplorerMedium
  IE.Visible = True
  IE.Navigate "http://appln.ABC.com/index.epl"

  Do While IE.ReadyState <> 4: DoEvents: Loop

  Set UserName = IE.Document.getElementById("login_usr")
  UserName.Value = "ABCDE"

  Set Password = IE.Document.getElementById("login_password")
  Password.Value = "ABCDE"

  Set btnLogin = IE.Document.getElementById("Login")
  btnLogin.Click

End Sub

In the linked example they only used one object, it seems you don't need to create objects for everything you're doing. So, you could probably get away with:

Sub Website()

  Dim IE As Object

  Set IE = New InternetExplorerMedium
  IE.Visible = True
  IE.Navigate "http://appln.ABC.com/index.epl"

  Do While IE.ReadyState <> 4: DoEvents: Loop

  With IE.Document
      .getElementById("login_usr").Value = "ABCDE"
      .getElementById("login_password").Value = "ABCDE"
      .getElementById("Login").Click
  End With

End Sub

You might also add a second do while loop for ready state, as in that linked example.

Community
  • 1
  • 1
n8.
  • 1,732
  • 3
  • 16
  • 38
1
Sub website()
    Dim ie As Variant
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True
    ie.navigate "http://appln.ABC.com/index.epl"
    Do While ie.readyState <> 4: DoEvents: Loop
    ie.document.getElementById("login_usr").Value = "ABCDE"
    ie.document.getElementById("login_password").Value = "ABCDE"
    ie.document.getElementById("Login").Click
End Sub
Karthick Gunasekaran
  • 2,697
  • 1
  • 15
  • 25
  • I didn't look at your answer until I was done with mine, looks like pretty much the same thing. Can you explain the purpose of the `IHTMLElement`? I don't see it being used. – n8. Apr 06 '16 at 17:50
  • Hi karthick - When I try your code, I am getting "Automation error: The object invoked has disconnected from its clients" error. – Stratos7 Apr 07 '16 at 04:01
  • @n8. IHTML has never used here hence removed as per your advice.Thanks – Karthick Gunasekaran Apr 07 '16 at 05:29
  • Tried the code with no result. Error --> "Automation Error. Interface is unknown." – Stratos7 Apr 08 '16 at 05:16
  • try this `Set IE = New InternetExplorerMedium` instead of `Set ie = CreateObject("InternetExplorer.Application")` and it requires Microsoft Internet Controls – Karthick Gunasekaran Apr 08 '16 at 10:26
  • That's what my answer does and it's not working for OP – n8. Apr 08 '16 at 17:27
0

I am not sure how but when I set

 IE.Visible = False

I don't get the error anymore. Please do try and if possible explain why it happens..

akilesh raj
  • 656
  • 1
  • 8
  • 19
0

I was having the same problem and found another possible solution. I was checking IE.Busy in my wait loop. I ended up with the same error. It seems that Excel is not finding a Document element in the IE object initially. When I would wait about 1 minute then continue the code, the error would stop happening. I found the following code gave enough time to ensure the document element was created and Excel could find it to start finding elements.

Do While IE.Busy
    Do Events
Loop

Do While IE.readyState <> 4
    Do Events
Loop
Paul Roub
  • 36,322
  • 27
  • 84
  • 93