我不抱希望,但我想知道是否有办法让vba脚本识别SAP GUI抛出的错误。 例如,如果一个事务被另一个用户锁定。目前,如果这个错误弹出,脚本什么也不考虑,继续下一行(通常会出错,因为我让它按的按钮还不存在)。如果可能的话,我想在下一行之前识别出这个错误,这样用户就可以修复它。
在某些情况下,如果做了变通,我会让我的脚本假设错误,再加上一个 on error resume next
但这让我感到恶心,而且并不适合所有情况。
举个例子。
For Each cell In Range("D:D")
If cell.Value = "" Then Exit For
session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010/ctxtMAPL-MATNR[2," & 4 & "]").Text = cell.Value
session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010/txtMAPL-PLNAL[0," & 4 & "]").Text = groupctr
session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010/ctxtMAPL-WERKS[3," & 4 & "]").Text = plant
session.findById("wnd[1]/usr/tblSAPLCZDITCTRL_4010").verticalScrollbar.Position = i + 1
'If a duplicate entry is found, an SAP message box pops up. The script then closes the message box. If no message box pops up,
'the script errors when trying to close it. Therefore, an "error" means the item was sucesfully added.
On Error Resume Next
session.findById("wnd[2]/tbar[0]/btn[0]").press
Next cell
为了清楚起见,我想得到这样的东西:
On Error Goto ErrHndl
'...my code...
exit sub
ErrHdnl:
Select Case SAPErrCode
Case [ExCode1]
'do things for this error type
Case [ExCode2]
'do different things for this error type