Search code examples
vbainternet-explorerautomation

Excel VBA error while check logon and logout check using IE


I am amature VBA programmer. I am working in a small automation. The steps are

  1. Open a IE browser
  2. Navigate to a webpage
  3. Logon using my username and password

I am using below excel VBA code to get my solution

Code snippet

Sub LoginAuto()
Dim Myhtml As IHTMLElement
Dim Myurl As String
Myurl = "http://myurl.com"
Set Mybroswer = New InternetExplorer
Mybroswer.Silent = True
Mybroswer.navigate Myurl
Mybroswer.Visible = True

Do
Loop Until Mybroswer.readyState = READYSTATE_COMPLETE
Set HTMLDOC = Mybroswer.document

HTMLDOC.all.UserName.Value = "myuser"
HTMLDOC.all.Password.Value = "mypassword"

For Each Myhtml In HTMLDOC.getElementsByTagName("button")
    If Myhtml.Type = "submit" Then
    Myhtml.Click:
    End If
    Exit For
Next
End Sub

while running above program i am getting below error

excel vba automation error the object invoked has disconnected from its clients


Solution

  • Dears, I am able to resolve my reported problem by using "SHDocVw.InternetExplorer" instead of IHTMLElement

    This allows me to "Loop Until Mybroswer.readyState = READYSTATE_COMPLETE"