首页 > 解决方案 > 尝试对 SAP 应用程序执行操作时出现 COM 错误

问题描述

我们正在使用 Python pywin32 com 库来编写在 Windows 上运行的 SAP GUI 应用程序的脚本。

事情一直到昨天。

现在,在尝试访问下面执行的代码行时maximize(),我们得到

com_error: (-2147417851, '服务器抛出异常。', None, None)

并在尝试访问 SAP 窗口中的任何对象(代码的最后一行)时出现以下错误。

AttributeError:无法设置属性“.text”。

有人可以帮忙吗?如果需要更多信息,请告诉我。

下面是我们用来获取新脚本会话、启动 SAP 并执行操作的代码片段:

from subprocess import call
import win32com.client
import time

GUIPath = 'C:/Program Files (x86)/SAP/FrontEnd/SAPgui/'
WinTitle = 'SAP'
SID = 'xxxxxx.sap.xxxxx.com'
InstanceNo = 'xx'

shell = win32com.client.Dispatch("WScript.Shell")
cmdString = os.path.join(GUIPath, 'SAPgui.exe') + " " + SID + " " + InstanceNo
call(cmdString)
while not shell.AppActivate(WinTitle):
 time.sleep(1)

checkGUIExists = False
while not checkGUIExists:
  try:
    SAP = win32com.client.GetObject("SAPGUI").GetScriptingEngine
    session = SAP.FindById("/app/con[0]/ses[0]") # session
    checkGUIExists = True
 except:
  time.sleep(1)
 continue

//The lines failing//
session.findById("wnd[0]").maximize()
session.findById("wnd[0]/tbar[0]/okcd).text = <transaction>

标签: pythoncompywin32sap-gui

解决方案


推荐阅读