VBA + Excel +试用Catch

HSh*_*ick 17 excel vba excel-vba

在VBA中,我正在编写一个简单的脚本来记录正在使用的电子表格的版本.

Private Sub Workbook_Open()
    version = "1.0"

    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    URL = "<WEB SERVICE>"

    objHTTP.Open "POST", URL, False
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    objHTTP.send ("version=" + version)
End Sub     
Run Code Online (Sandbox Code Playgroud)

流程工作找到.

我试图尝试捕获所以如果Web主机离线,而不是显示运行时错误,我抓住它并抑制.

尝试在VBA中捕获的最佳方法是什么,因此没有显示错误消息?

Tre*_*vor 24

Private Sub Workbook_Open()
    on error goto Oops
    version = "1.0"

    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    URL = "<WEB SERVICE>"

    objHTTP.Open "POST", URL, False
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    objHTTP.send ("version=" + version)
    exit sub
Oops:
    'handle error here
End Sub   
Run Code Online (Sandbox Code Playgroud)

例如,如果您因为错误而更改了URL,则可以执行此操作

Private Sub Workbook_Open()
    on error goto Oops
    version = "1.0"

    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    URL = "<WEB SERVICE>"
Send:
    objHTTP.Open "POST", URL, False
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    objHTTP.send ("version=" + version)
    exit sub
Oops:
    'handle error here
    URL="new URL"
    resume Send 'risk of endless loop if the new URL is also bad
End Sub   
Run Code Online (Sandbox Code Playgroud)

此外,如果你的感觉真的尝试/吸引人,你可以仿效这样.

Private Sub Workbook_Open()
    version = "1.0"

    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    URL = "<WEB SERVICE>"
    on error resume next 'be very careful with this, it ignores all errors
    objHTTP.Open "POST", URL, False
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    objHTTP.send ("version=" + version)
   if err <> 0 then
      'not 0 means it errored, handle it here
      err.clear 'keep in mind this doesn't reset the error handler, any code after this will still ignore errors
   end if
End Sub  
Run Code Online (Sandbox Code Playgroud)

因此,将其扩展为真正的核心......

Private Sub Workbook_Open()
    version = "1.0"
    on error resume next
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    if err <> 0 then
        'unable to create object, give up
        err.clear
        exit sub
    end if
    URL = "<WEB SERVICE>"
    objHTTP.Open "POST", URL, False
    if err <> 0 then
        'unable to open request, give up
        err.clear
        exit sub
    end if
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    objHTTP.send ("version=" + version)
   if err <> 0 then
      'unable to send request, give up
      err.clear
      exit sub
   end if
End Sub  
Run Code Online (Sandbox Code Playgroud)

另外值得注意的是,on error goto风格中发生的任何错误都不会被处理,所以如果你这样做了

private sub MakeError()
   dim iTemp as integer
   on error goto Oops
   iTemp = 5 / 0 'divide by 0 error
   exit sub
Oops:
   itemp = 4 / 0 'unhandled exception, divide by 0 error
end sub
Run Code Online (Sandbox Code Playgroud)

但是,会导致未处理的异常

private sub MakeError()
   dim iTemp as integer
   on error resume next
   iTemp = 5 / 0 'divide by 0 error
   if err <> 0 then
       err.clear
       iTemp = 4 / 0 'divide by 0 error, but still ignored
       if err <> 0 then
           'another error
       end if
   end if
end sub
Run Code Online (Sandbox Code Playgroud)

不会导致任何异常,因为VBA忽略了它们.


小智 6

像这样的东西:

Try
    ...
Catch (Exception e)
    ...
End Try
Run Code Online (Sandbox Code Playgroud)

在 VBA 中可能看起来像这样:

' The "Try" part
On Error Resume Next
...
On Error GoTo 0
' The "Catch" part
If Err.Number <> 0 Then
...
End If
Run Code Online (Sandbox Code Playgroud)

但是,此表单可能未遵循最佳实践。

  • 继续下一步意味着它会在检查错误之前尝试运行块中的所有代码。如果你小心,这可能没问题。但是您只能安全地在块中进行一项操作。例如,如果您读取一个文件,附加并写回它。但是文件读取错误,您将尝试附加任何内容,并仅用附加的结果写回,擦除原始内容。所以这两个文件操作都需要在它们自己的块中才能让这个方法工作。只是需要注意的事情。 (3认同)