【发布时间】:2021-01-09 22:33:11
【问题描述】:
我正在尝试创建一个函数,该函数通过使用跟踪号来获取空运单的状态。 在 stackoverflow 社区的帮助下,我设法创建了一个正确获取状态的函数。
但是,我正在尝试添加跟踪号可能不正确的错误处理。 使用当前函数,如果跟踪号有效,它会正确获取结果。 但是当提供的数字不正确时,该函数会返回一个 0 值并在后台循环运行。从 VBA 编辑器停止时,excel 崩溃。
这是迄今为止我想出的代码。任何添加此错误处理的帮助将不胜感激。 样品正确货号:92366691 样品错误货号:59473805
Function FlightStat_AF(cargoNo As Variant) As String
Dim url As String, ie As Object, result As String
url = "https://www.afklcargo.com/mycargo/shipment/detail/057-" & cargoNo
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = False
.navigate url
Do Until .readyState = 4: DoEvents: Loop
End With
'wait a little for dynamic content to be loaded
Application.Wait (Now + TimeSerial(0, 0, 1))
'Get the status from the table
Do While result = ""
DoEvents
On Error Resume Next
result = Trim(ie.document.getElementsByClassName("fs-12 body-font-bold")(1).innerText)
On Error GoTo 0
Application.Wait (Now + TimeSerial(0, 0, 1))
Loop
ie.Quit: Set ie = Nothing
'Return value of the function
FlightStat_AF = result
End Function
【问题讨论】:
标签: excel vba web-scraping error-handling