Excel VBAlogin到SAP和提取电子表格 – 在Excel中卡住

我有一个Excel VBA代码的问题,当我执行代码时,它一直处于“运行”状态,请参阅Stuck Excel图;

代码的目的是:

  1. login到SAP
  2. 执行交易(在这种情况下是IW73)
  3. 将电子表格导出为.txt

closuresSAP会话后Excel的问题卡在“正在运行”。 我们已经尝试在不同的计算机上运行它(陷入“运行”)错误。

码:

Sub Logontrial() Dim SapGuiApp As Object Dim oConnection As Object Dim SAPCon As Object, SAPSesi As Object Dim SapGuiAuto As Object, SAPApp As Object If SapGuiApp Is Nothing Then Set SapGuiApp = CreateObject("Sapgui.ScriptingCtrl.1") End If If oConnection Is Nothing Then Set oConnection = SapGuiApp.OpenConnection("5.1.1 AP1 ERP Production", True) End If If SAPSesi Is Nothing Then Set SAPSesi = oConnection.Children(0) End If Application.DisplayAlerts = False With SAPSesi SAPSesi.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "500" SAPSesi.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "UserName" SAPSesi.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "Password" SAPSesi.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN" SAPSesi.findById("wnd[0]/usr/txtRSYST-LANGU").SetFocus SAPSesi.findById("wnd[0]/usr/txtRSYST-LANGU").caretPosition = 2 SAPSesi.findById("wnd[0]").sendVKey 0 ' start extraction On Error GoTo Resume1 ' DoEvents SAPSesi.findById("wnd[0]").maximize SAPSesi.findById("wnd[0]/tbar[0]/okcd").Text = "/nIW73" SAPSesi.findById("wnd[0]").sendVKey 0 SAPSesi.findById("wnd[0]/usr/ctxtSWERK-LOW").Text = "GB10" SAPSesi.findById("wnd[0]/usr/ctxtSWERK-LOW").SetFocus SAPSesi.findById("wnd[0]/usr/ctxtSWERK-LOW").caretPosition = 4 SAPSesi.findById("wnd[0]").sendVKey 8 SAPSesi.findById("wnd[0]").sendVKey 0 SAPSesi.findById("wnd[0]/mbar/menu[0]/menu[11]/menu[2]").Select SAPSesi.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select SAPSesi.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").SetFocus SAPSesi.findById("wnd[1]/tbar[0]/btn[0]").press SAPSesi.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "DataImport1.txt" SAPSesi.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 15 SAPSesi.findById("wnd[1]/tbar[0]/btn[11]").press SAPSesi.findById("wnd[0]/tbar[0]/okcd").Text = "/n" SAPSesi.findById("wnd[0]").sendVKey 0 Set SAPSesi = Nothing Set oConnection = Nothing Set SapGuiApp = Nothing End With ' This part after closing the SAP session it get stuck. Resume1: Application.DisplayAlerts = True Set SAPSesi = Nothing Set oConnection = Nothing Set SapGuiApp = Nothing Exit Sub End Sub 

提前致谢

//帕特里克

免责声明: 这不是高质量的代码,很可能有一天有人会对你施加一些仇恨。

在find更好的东西之前,尝试以下内容,它应该可以工作。 简单地写在这里End

 Set SAPSesi = Nothing Set oConnection = Nothing Set SapGuiApp = Nothing End 

然后寻找更好的解决scheme。