我有代码从ThisWorkbook中的多个列中获取数据,并在Internet Explorer的网站中放入各种字段。点击第1行(搜索按钮)后加载网站。然后代码在第2行抛出一个错误,它点击复选框,因为如果网站仍在加载,则没有复选框。 (我认为该网站建立在Sharepoint上并且编码很差。)
是否有任何代码在2-3秒后重复第2行并在错误出现时继续进行?我尝试了错误处理程序重复代码,但没有工作。
Sub CSA_Upload()
Dim test1 As Long, test2 As Long
test1 = Timer
Dim n As Long
Range("A1").Select
n = Selection.End(xlDown).Row
ThisWorkbook.Sheets("Data").Range("A2:A" & n).Interior.ColorIndex = 0
Dim IE As Object
Dim doc As Object
Dim htmlTable As htmlTable
Set IE = New InternetExplorerMedium
'Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
'Navigate to CSA tool Home Page
IE.navigate "https://csa.abcdefg.com/Collector_view.aspx/"
'Wait till it loads
Do While IE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = CreateObject("htmlfile")
Set doc = IE.document
'Enter Invoice Number in SearchBy box
doc.getElementById("ContentPlaceHolder1_ddlSearch").Value = "[Inv Number]"
Range("A1").Select
'Count the number of rows in the data list
Dim X As Long
Range("A1").Select
X = Selection.End(xlDown).Row
'For each invoice number the loop starts here
For rowNo = 2 To X
ActiveCell.Offset(1).Select
'Fill Blue colour in active processing invoice number cell
ThisWorkbook.Sheets("Data").Range("A" & rowNo).Interior.ColorIndex = 37
'Input the invoice number
doc.getElementById("ContentPlaceHolder1_txtSearch").Value = ThisWorkbook.Sheets("Data").Range("A" & rowNo).Value
'Click the Search button
'This is the Line1
doc.getElementById("ContentPlaceHolder1_search").Click
'Wait till it loads
Do While IE.Busy
Application.Wait DateAdd("s", 5, Now)
Loop
'Checkbox select all
'This is the Line2
doc.getElementById("ContentPlaceHolder1_GridView1_chkboxSelectAll").Click
'Wait 3 seconds till it selects all the checkboxes
Application.Wait DateAdd("s", 3, Now)
'Enter rest of the data
doc.getElementById("ContentPlaceHolder1_ddlaction").Value = ThisWorkbook.Sheets("Data").Range("B" & rowNo).Value 'Input Action
doc.getElementById("ContentPlaceHolder1_txtToDoDate").Value = ThisWorkbook.Sheets("Data").Range("C" & rowNo).Value 'Input Action Date
doc.getElementById("ContentPlaceHolder1_ddlstatus").Value = ThisWorkbook.Sheets("Data").Range("D" & rowNo).Value 'Input Root Cause
doc.getElementById("ContentPlaceHolder1_txtcomments").Value = ThisWorkbook.Sheets("Data").Range("E" & rowNo).Value 'Input Comments
doc.getElementById("ContentPlaceHolder1_btn_Comments").Click 'Click Submit button
Application.Wait DateAdd("s", 3, Now)
'Hit enter on MessegeBox
Application.SendKeys "{ENTER}"
'Fill Green colour in the active cell when all entries are passed
ThisWorkbook.Sheets("Data").Range("A" & rowNo).Interior.ColorIndex = 35
Next 'Proceed to next invoice number
IE.Quit 'Quit Internet explorer
test2 = Timer
MsgBox (X - 1) & " Invoices have been updated and it took " & Format((test2 - test1) / 86400, "hh:mm:ss") & " Seconds."
End Sub
我认为错误是因为doc
被改变了。
改写
' This is the Line2
doc.getElementById("ContentPlaceHolder1_GridView1_chkboxSelectAll").Click
'Wait 3 seconds till it selects all the checkboxes
Application.Wait DateAdd("s", 3, Now)
如
' This is the Line2
application.wait Application.Wait DateAdd("s", 1, Now)
set doc = IE.document
doc.getElementById("ContentPlaceHolder1_GridView1_chkboxSelectAll").Click
'Wait 3 seconds till it selects all the checkboxes
Application.Wait DateAdd("s", 3, Now)
也许有帮助。
在每个.Navigate
和.Click
之后使用正确的页面加载等待。
While ie.Busy Or ie.readyState < 4: DoEvents: Wend
此外,您可以在尝试设置对象引用的定时循环中包装与定时相关的抛出错误的元素
Dim t As Date, ele As Object
Const MAX_WAIT_SEC As Long = 10
t = Timer
Do
On Error Resume Next
Set ele = doc.getElementById("ContentPlaceHolder1_GridView1_chkboxSelectAll")
On Error GoTo 0
If Timer - t > MAX_WAIT_SEC Then Exit Do
Loop While ele Is Nothing
If Not ele Is Nothing Then
ele.Click
End If
我在第1行之后删除了下面的等待循环。
'Wait till it loads
Do While IE.Busy
Application.Wait DateAdd("s", 5, Now)
Loop
并在之前添加了修复10秒等待时间Application.Wait DateAdd("s", 10, Now)
doc.getElementById("ContentPlaceHolder1_GridView1_chkboxSelectAll").Click
'Wait 3 seconds till it selects all the checkboxes
Application.Wait DateAdd("s", 3, Now)
所以最后一段代码如下,它正在工作!
'This is the Line1
doc.getElementById("ContentPlaceHolder1_search").Click
'Checkbox select all
'This is the Line2
Application.Wait DateAdd("s", 10, Now)
doc.getElementById("ContentPlaceHolder1_GridView1_chkboxSelectAll").Click
'Wait 3 seconds till it selects all the checkboxes
Application.Wait DateAdd("s", 3, Now)