1

我有一个 Excel VBA 代码的问题,当我执行代码时,它会一直卡在“运行”中,见图卡住Excel;

代码的目的是:

  1. 登录 SAP
  2. 执行交易(在本例中为 IW73)
  3. 将电子表格导出为 .txt

关闭 SAP 会话后的问题 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

提前致谢

//帕特里克

4

1 回答 1

0

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

在找到更好的东西之前,请尝试以下操作,它应该可以工作。简单写End在这里:

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

然后寻找更好的解决方案。

于 2017-07-20T08:57:28.787 回答