0

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

James Z
  • 12,209
  • 10
  • 24
  • 44
Guru
  • 1
  • 2
  • What line is throwing the error? – TinMan Jul 24 '20 at 07:01
  • Loop Until Mybroswer.readyState = READYSTATE_COMPLETE if I comment above line error is on Set HTMLDOC = Mybroswer.document – Guru Jul 24 '20 at 08:31
  • You could add `DoEvents` between `Do` and `Loop Until Mybroswer.readyState = READYSTATE_COMPLETE`. For more information, you could refer to [this thread](https://stackoverflow.com/questions/42403210/internet-explorer-vba-automation-error-the-object-invoked-has-disconnected-from). You could also try to use `Do Until ieA.readyState = 4 DoEvents Loop`. – Yu Zhou Jul 27 '20 at 02:45

2 Answers2

0

Is Autoit (www-autoitscript.com) an option for you? It provides lots of useful functions to automate stuff like that.

Try this

#include<IE.au3>
$sUsername = "Username"
$sPassword = "Password"
$sUrl = "http://www.yoururl.com"
$oIE = _IECreate($sUrl, 0, 1, 1, 1)
$oHWND = _IEPropertyGet($oIE, "hwnd")
WinSetState($oHWND, "", @SW_MAXIMIZE)
$oForm = _IEFormGetCollection ($oIE, 0)
$oUsername = _IEFormElementGetObjByName($oForm, 'UserName')
$oPassword = _IEFormElementGetObjByName($oForm, "Password")
_IEFormElementSetValue($oUsername, $sUsername)
_IEFormElementSetValue($oPassword, $sPassword)
_IEFormSubmit ($oForm)
Xenobiologist
  • 2,091
  • 1
  • 12
  • 16
0

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"

Guru
  • 1
  • 2