VBA + Excel +试试看

在VBA中,我正在做一个简单的脚本来logging正在使用的电子表格的版本。

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 

过程作品find。

我试图做一个尝试赶上,所以如果networking主机脱机,而不是显示运行时错误,我抓住它并压制。

在VBA中尝试catch的最好方法是什么,所以没有显示错误信息?

 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 

如果你想,例如,由于错误更改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 

另外,如果你的感觉真的很吸引人,你可以像这样效仿。

 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 

所以把这个扩展到真正的核心

 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 

另外值得注意的是,任何错误发生在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 

但是会导致一个未处理的exception

 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 

不会引起任何exception,因为VBA忽略了它们。