繁体   English   中英

Excel 一直在“运行”,同时要求 SAP GUI 导出电子表格

[英]Excel keeps “running” while asking SAP GUI to export spreadsheet

我的 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

提前致谢

//帕特里克

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

在找到更好的东西之前,请尝试以下操作,它应该可以工作。 只需在此处写End

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM