2013-06-19 215 views
2

我有我的宏精美的工作,但我需要的循环运行100次和第2次之后,我得到刷新表数据

Run-time error '-2147417848 (80010108)': 

Automation error 
The object invoked has disconnected from its clients. 

我已经试过以下选项:

Application.CalculateFullRebuild 
'ActiveSheet.EnableCalculation = False 
'ActiveSheet.EnableCalculation = True 
'Application.Calculation = xlCalculationManual 
'Application.Calculation = xlCalculationAutomatic 
'Application.Wait Time + TimeSerial(0, 0, 1) 
'ActiveSheet.Calculate 

但他们都会循环两次,并在第三次我得到上述错误。以下是我的代码。

Private Sub Populate_Click() 
    Dim i As Long 
    Dim a As Long 
    Dim x As Long 
    Dim b As Long 

    Dim IE As Object 
    Dim objElement As Object 
    Dim objElementA As Object 
    Dim objCollection As Object 
    Dim objCollectionA As Object 


x = 0 
While x < 101 

Set IE = CreateObject("InternetExplorer.Application") 
IE.Visible = True 
IE.Navigate "website" 'make sure you've logged into the page 

Do 
DoEvents 
Loop Until IE.READYSTATE = 3 
Do 
DoEvents 
Loop Until IE.READYSTATE = 4 

Application.CalculateFullRebuild 
'ActiveSheet.EnableCalculation = False 
'ActiveSheet.EnableCalculation = True 
'Application.Calculation = xlCalculationManual 
'Application.Calculation = xlCalculationAutomatic 
'Application.Wait Time + TimeSerial(0, 0, 1) 
'ActiveSheet.Calculate 

Call IE.document.getelementbyid("name").SetAttribute("value", ActiveSheet.Range("b2").Value) 
Call IE.document.getelementbyid("aw_login").SetAttribute("value", ActiveSheet.Range("a2").Value) 

Set objCollection = IE.document.getElementsByTagName("input") 
i = 0 
While i < objCollection.Length 
    If objCollection(i).Type = "button" And _ 
     objCollection(i).Value = "Prefill" Then 
      Set objElement = objCollection(i) 
    End If 
    i = i + 1 
Wend 
objElement.Click 

Application.Wait Time + TimeSerial(0, 0, 2) 

Set objCollectionA = IE.document.getElementsByTagName("input") 
a = 0 
While a < objCollectionA.Length 
    If objCollectionA(a).Type = "submit" And _ 
     objCollectionA(a).Value = "OK" Then 
      Set objElementA = objCollectionA(a) 
    End If 
    a = a + 1 
Wend 
objElementA.Click 

Do 
DoEvents 
Loop Until IE.READYSTATE = 3 
Do 
DoEvents 
Loop Until IE.READYSTATE = 4 
IE.Quit 
x = x + 1 
Wend 

End Sub 

回答

0
Do 
DoEvents 
Loop Until IE.READYSTATE = 3 

我删除的代码位,现在它只是运行,因为它应该。