2017-02-22 63 views
0

当前正尝试构建一个VBA,将Excel中的数据迁移到SAP中。我想要的是: 如果没有错误发生,请保存订单号码(由SAP生成)并使用“UPDATED”填充旁边的单元格,然后转到SAP中的下一行。如果发生错误,请转至err_handler,保存错误状态(由SAP生成),并用“ERROR”填充其旁边的单元格,然后转到SAP中的下一行。 但是,对于这两个代码,我需要使用“下一个”,而我只有1个“For”。问题是,err_handling行被拾取(代码结束),即使顺序正常。我在这里做错了什么?我应该把err_handling放在别的地方吗?On Error GoTo与2x下一个循环(错误1x,正常过程1x)

此外,还有一个问题,弹出窗口现在称为wnd [1],但是有没有办法找到弹出窗口的真实名称,以便宏不仅自动点击每一个弹出?

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\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/ssubHEADER_FRAME:SAPMV45A:4440/ctxtRV45A-KETDAT").Text = col8 
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/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\01/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\01/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\01/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\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/subSUBSCREEN_BUTTONS:SAPMV45A:4050/btnBT_PKON").press 
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\05/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/ctxtKOMV-KSCHL[1,20]").Text = "YMCP" 
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\05/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/txtKOMV-KBETR[3,20]").Text = col11 
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\05/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/ctxtRV61A-KOEIN[4,20]").Text = "USD" 
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\05/ssubSUBSCREEN_BODY:SAPLV69A:6201/tblSAPLV69ATCTRL_KONDITIONEN/txtKOMV-KPEIN[5,20]").Text = col12 
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_ITEM/tabpT\05/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 
+2

请允许我向您介绍我的Rubberduck及其[智能压头](http://www.rubberduckvba.com/indentation)工具。加载你的IDE到你的IDE,然后去* Rubberduck>缩进>当前模块*。或者调出* Code Explorer *并右键单击项目节点并选择*缩进*以自动缩进整个项目。谢谢你以后。 –

回答

1

我想你可能是下面的“架构”之后:

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 
+1

@Thijsk,你通过它了吗? – user3598756

+0

Hi @ user3598756,感谢您的输入。第一次没有工作,然后我意识到我自己把代码放在某个地方犯了一个错误。现在它像一个魅力!万分感谢 :)! – Thijsk

+1

不客气。好的编码! – user3598756

2

只是测试,看看你是否达到了该代码由于错误:

 '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