On Error GoTo in loop with 2x next (1x on error, 1x on normal procedure)

On Error GoTo in loop with 2x next (1x on error, 1x on normal procedure)

目前正在尝试构建一个 VBA 以将来自 Excel 的数据迁移到 SAP。我想要的是以下内容: 如果没有错误发生,保存订单号(由 SAP 生成)并用 "UPDATED" 填充它旁边的单元格,然后转到 SAP 中的下一行。如果发生错误,转到 err_handler,保存错误状态(由 SAP 生成)并用 "ERROR" 填充它旁边的单元格,然后转到 SAP 中的下一行。 但是,对于这两个代码,我都需要使用"next",而我只有1个"For"。问题是,err_handling 行正在被拾取(代码末尾),即使订单没有问题。我在这里做错了什么?我应该把 err_handling 放在别的地方吗?

另外还有一个问题,弹窗windows现在叫wnd[1],但是有没有办法找到弹窗window的真实姓名,所以宏不只是自动点击每个弹出窗口?

Sub CREATE_Sales_Order()

Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
Set sapapp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set sapCon = sapapp.Children(0) 'Get the first system that is currently connected
Set session = sapCon.Children(0) 'Get the first session (window) on that connection
Set aSheet = ActiveSheet

On Error GoTo err_handling

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nva01"
session.findById("wnd[0]").sendVKey 0

For i = 2 To aSheet.UsedRange.Rows.Count
If aSheet.Cells(i, 12).Value <> "UPDATED" Then
col1 = Trim(CStr(aSheet.Cells(i, 1).Value))   'Column1 Order type
col2 = Trim(CStr(aSheet.Cells(i, 2).Value))   'Column2 Sales Org
col3 = Trim(CStr(aSheet.Cells(i, 3).Value))   'Column3 Distribution Channel
col4 = Trim(CStr(aSheet.Cells(i, 4).Value))   'Column4 Division
col5 = Trim(CStr(aSheet.Cells(i, 5).Value))   'Column5 Customer order number
col6 = Trim(CStr(aSheet.Cells(i, 6).Value))   'Column6 Sold-to
col7 = Trim(CStr(aSheet.Cells(i, 7).Value))   'Column7 Ship-to
col8 = Trim(CStr(aSheet.Cells(i, 8).Value))   'Column8 Requested Delivery Date
col9 = Trim(CStr(aSheet.Cells(i, 9).Value))   'Column9 Material Number
col10 = Trim(CStr(aSheet.Cells(i, 10).Value)) 'Column10 Material Quantity
col11 = Trim(CStr(aSheet.Cells(i, 11).Value))  'Column11 Price
col12 = Trim(CStr(aSheet.Cells(i, 12).Value)) 'Column12 KG

On Error GoTo err_handling

'First entry screen
session.findById("wnd[0]/usr/ctxtVBAK-AUART").Text = col1 
session.findById("wnd[0]/usr/ctxtVBAK-VKORG").Text = col2 
session.findById("wnd[0]/usr/ctxtVBAK-VTWEG").Text = col3 
session.findById("wnd[0]/usr/ctxtVBAK-SPART").Text = col4 
'Session.findbyid("wnd[0]/usr/ctxtVBAK-SPART").SetFocus
'Session.findbyid("wnd[0]/usr/ctxtVBAK-SPART").caretPosition = 2
session.findById("wnd[0]").sendVKey 0

'Second entry screen (actual order)
session.findById("wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/txtVBKD-BSTKD").Text = col5 '
session.findById("wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/subPART-SUB:SAPMV45A:4701/ctxtKUAGV-KUNNR").Text = col6 
session.findById("wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/subPART-SUB:SAPMV45A:4701/ctxtKUWEV-KUNNR").Text = col7 
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT/ssubSUBSCREEN_BODY:SAPMV45A:4400/ssubHEADER_FRAME:SAPMV45A:4440/ctxtRV45A-KETDAT").Text = col8 
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/ctxtRV45A-MABNR[1,0]").Text = col9 
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtRV45A-KWMENG[2,0]").Text = col10 '"20000"
'Session.findbyid("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtRV45A-KWMENG[2,0]").SetFocus
'Session.findbyid("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/tblSAPMV45ATCTRL_U_ERF_AUFTRAG/txtRV45A-KWMENG[2,0]").caretPosition = 19
session.findById("wnd[0]").sendVKey 0
'If the bill-to pop-up exists, automatically click it away
If session.ActiveWindow.Name = "wnd[1]" Then
'session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
session.findById("wnd[1]").sendVKey 0
End If
'Continue the scheduling window
session.findById("wnd[0]/shellcont/shell/shellcont[0]/shell").pressButton "CONT"
'If the price needs to be changed, do this
If aSheet.Cells(i, 11).Value <> "" Then
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/subSUBSCREEN_BUTTONS:SAPMV45A:4050/btnBT_PKON").press
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/ctxtKOMV-KSCHL[1,20]").Text = "YMCP"
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/txtKOMV-KBETR[3,20]").Text = col11 
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/ctxtRV61A-KOEIN[4,20]").Text = "USD"
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/txtKOMV-KPEIN[5,20]").Text = col12 
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/ctxtKOMV-KMEIN[6,20]").Text = "KG"
session.findById("wnd[0]").sendVKey 0
End If
session.findById("wnd[0]/tbar[0]/btn[11]").press
'If the Dynamic credit check pop-up exists, automatically click it away
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]").sendVKey 0
End If

'report the orderstatus back to excel
m_status = session.findById("wnd[0]/sbar").Text
If m_status <> "" Then
    aSheet.Cells(i, 13) = m_status

aSheet.Cells(i, 14) = "UPDATED"
End If

session.findById("wnd[0]/tbar[0]/okcd").Text = "/nva01"
session.findById("wnd[0]").sendVKey 0
End If


'If an error occurs, show that in the sheet, macro now crashes when there is a second error, still needs fix
err_handling: 'report the orderstatus back to excel
m_status = session.findById("wnd[0]/sbar").Text
aSheet.Cells(i, 13) = m_status
aSheet.Cells(i, 14) = "Error"
'open new order
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nva01"
session.findById("wnd[0]").sendVKey 0

Next

MsgBox "All orders have been entered into SAP"

End Sub

只需测试一下是否由于错误而到达该代码:

        'If an error occurs, show that in the sheet, macro now crashes when there is a second error, still needs fix
err_handling:                                    'report the orderstatus back to excel
        If Err.Number <> 0 Then  '<-- Test for error.
            m_status = session.findById("wnd[0]/sbar").Text
            aSheet.Cells(i, 13) = m_status
            aSheet.Cells(i, 14) = "Error"
            'open new order
            session.findById("wnd[0]/tbar[0]/okcd").Text = "/nva01"
            session.findById("wnd[0]").sendVKey 0
            Err.Clear
        End If
    Next

我想你可能会在以下 "architecture":

Sub CREATE_Sales_Order()

    Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
    ' ...
    ' your "Setting" code section
    ' ...

    On Error GoTo err_handling

    session.findById("wnd[0]").maximize
    session.findById("wnd[0]/tbar[0]/okcd").Text = "/nva01"
    session.findById("wnd[0]").sendVKey 0

begin_Loop: '<--|place this label to be referenced by 'err_handling' code should this latter be reached before entering the loop

    For i = 2 To aSheet.UsedRange.Rows.Count
        If aSheet.Cells(i, 12).Value <> "UPDATED" Then
            '...
            '... your code inside If-Then-EndIf 
            '    you can take 'On Error GoTo err_handling' statement off it
            '...
        End If

next_SAP_Line: '<--|place this label to be referenced by 'err_handling' code should this latter be reached while inside the loop

        'open new order
        session.findById("wnd[0]/tbar[0]/okcd").Text = "/nva01"
        session.findById("wnd[0]").sendVKey 0
    Next

    MsgBox "All orders have been entered into SAP"
    Exit Sub '<--| exit sub not to reach the subsequent 'err_handling' code 


    'If an error occurs, show that in the sheet, macro now crashes when there is a second error, still needs fix
err_handling:    'report the orderstatus back to excel
    m_status = session.findById("wnd[0]/sbar").Text
    aSheet.Cells(i, 13) = m_status
    aSheet.Cells(i, 14) = "Error"

    If i = 0 Then Resume begin_Loop '<--| if error happend before entering loop, the resume at loop start
    Resume next_SAP_Line '<--| if error happend inside loop, then resume at loop next itaration

End Sub