如何 运行 SAP 关闭或打开或未登录的 SAP 事务

How to run an SAP transaction with SAP closed or open or not logged in

我想做的很简单: 我有一个 运行 交易的脚本,我希望宏始终执行交易。

分三种情况:

  1. SAP 未打开(代码与之配合使用,第一部分正在启动)
  2. SAP 已打开并登录(代码也有效)
  3. SAP 已打开但未登录(这是代码失败的地方)

这是错误:

run-time error 614. 'The enumerator of the collection cannot find an element with the specified index'

(仅供参考,出于此处的目的,我假设没有密码登录,因此您只需按回车键)

'Launching SAP and logging into the main screen if the program is not already open
If IsProcessRunning("saplogon.EXE") = False Then
    Dim SapGui, Applic, connection, session, WSHShell
    
    Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus
    Set WSHShell = CreateObject("WScript.Shell")
    
    Do Until WSHShell.AppActivate("SAP Logon ")
        Application.Wait Now + TimeValue("0:00:01")
    Loop
    
    Set WSHShell = Nothing
    Set SapGui = GetObject("SAPGUI")
    Set Applic = SapGui.GetScriptingEngine
    Set connection = Applic.OpenConnection("InsertTextHere", True)
    Set session = connection.Children(0)
    session.findById("wnd[0]").maximize
    session.findById("wnd[0]").sendVKey 0
End If
    
'Executing main screen, this part here is just setting variables for the logged in interface. So once this is done you can enter the transaction. 
    
If Not IsObject(XXX) Then
    Set SapGuiAuto = GetObject("SAPGUI")
    Set XXX = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
    Set connection = XXX.Children(0)
End If

'The error is here, when it tries to make session into connection.Children
If Not IsObject(session) Then
    Set session = connection.Children(0)

End If
If IsObject(WScript) Then
    WScript.ConnectObject session, "on"
    WScript.ConnectObject XXX, "on"
End If

'Transaction comes here

Function IsProcessRunning(process As String)
    Dim objList As Object
    
    Set objList = GetObject("winmgmts:") _
        .ExecQuery("select * from win32_process where name='" & process & "'")
    
    IsProcessRunning = objList.Count > 0
    
End Function



编辑:多一点上下文可能会有帮助。我制作了一个本应通过自动访问事务来节省人们时间的宏,我添加了说明“您必须在 运行 启用它之前已经登录到 SAP”。不幸的是,即使我在按钮旁边用亮红色字母显示文本,我每周也会收到几封电子邮件通知我有错误,而且每次都是有人没有打开 SAP 或有人没有登录英寸

这个话题显然仍然很有趣。带有 SingleSignOn 的版本特别具有 Exxonboy 描述的 3 种状态。缺失状态编号 2 可以识别如下。

例如:

'Launching SAP and logging into the main screen if the program is not already open

If IsProcessRunning("saplogon.EXE") = False Then
Dim SapGui, Applic, connection, session, WSHShell

Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus

Set WSHShell = CreateObject("WScript.Shell")

Do Until WSHShell.AppActivate("SAP Logon ")
    Application.Wait Now + TimeValue("0:00:01")
Loop

Set WSHShell = Nothing
Set SapGui = GetObject("SAPGUI")
Set Applic = SapGui.GetScriptingEngine
Set connection = Applic.OpenConnection("InsertTextHere", True)
Set session = connection.Children(0)
session.findById("wnd[0]").maximize
session.findById("wnd[0]").sendVKey 0
End If

'Executing main screen, this part here is just setting variables for the  logged in interface. So once this is done you can enter the transaction. 

'-----------new--------------------------
on error resume next
'-----------new--------------------------
If Not IsObject(XXX) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set XXX = SapGuiAuto.GetScriptingEngine
   '-----------new--------------------------
   myError = err.number
   '-----------new--------------------------
End If
If Not IsObject(connection) Then
   Set connection = XXX.Children(0)
   '-----------new--------------------------
   myError = err.number
   '-----------new--------------------------
End If

If Not IsObject(session) Then
   Set session = connection.Children(0)
   '-----------new--------------------------
   myError = err.number
   '-----------new--------------------------
End If
'-----------new--------------------------
on error goto 0
If myError <> 0 Then
  Set connection = XXX.OpenConnection("InsertTextHere", True)
  Set session = connection.Children(0)
  session.findById("wnd[0]").maximize
End If
'-----------new--------------------------

If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject XXX, "on"
End If

'Transaction comes here

Function IsProcessRunning(process As String)
Dim objList As Object

Set objList = GetObject("winmgmts:") _
    .ExecQuery("select * from win32_process where name='" & process & "'")

IsProcessRunning = objList.Count > 0

End Function

问候,ScriptMan