自动化错误:“调用的对象已与其客户端断开连接”

时间:2019-07-16 17:33:07

标签: html excel vba internet-explorer automation

我正在尝试使用在线代码从受密码保护的网站中提取数据,并且不断收到运行时错误-'2137317848 ....'

。错误始于包含以下内容的行:

Do Until ieApp.ReadyState = READYSTATE_COMPLETE

我不确定如何解决此问题。

Sub Getdata()
MyString = ActiveCell.Value
Application.Calculation = xlCalculationManual

Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject

'create a new instance of ie
Set ieApp = New InternetExplorer

'you don’t need this, but it’s good for debugging
ieApp.Visible = True

'assume we’re not logged in and just go directly to the login page
ieApp.navigate MyString
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

Set ieDoc = ieApp.Document

'fill in the login form – View Source from your browser to get the control names
With ieDoc.forms(0)
.login.Value = "myuserid"
.Password.Value = "pass"
.submit
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

'now that we’re in, go to the page we want
ieApp.navigate MyString
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

'get the table based on the table’s id
Set ieDoc = ieApp.Document
Set ieTable = ieDoc.getitembyClass.Item("primary-link")

'copy the tables html to the clipboard and paste to teh sheet
If Not ieTable Is Nothing Then
Set clip = New DataObject
clip.SetText "" & ieTable.outerHTML & ""
clip.PutInClipboard
Sheet1.Select
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
End If

'close 'er up
ieApp.Quit
Set ieApp = Nothing

End Sub

我希望通过类ID来获取数据,因此,如果看起来也不正确,请告诉我!

0 个答案:

没有答案