2013-06-19 188 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 

我刪除的代碼位,現在它只是運行,因爲它應該。