【问题标题】:VBA Sap Scripting to open or connect/remain in already open session windowVBA Sap Scripting 打开或连接/保留在已打开的会话窗口中
【发布时间】:2019-04-17 17:37:38
【问题描述】:

我可以通过使用 VBA 和 SAP 记录和播放 .vbs 脚本来自动化 SAP 事务数据检索,现在我遇到了一个我在 Visual Basic Studio .NET 的同一程序中没有看到的问题,如果 SAP Easy访问我的名称连接:LA-6-Prod 已经打开,然后出现 Multilogon 窗口并破坏我的代码,从而使整个事情失败。

如何让我的代码使用已经存在且打开的连接,或者如果它未打开,则打开它?

如果 LA-6-Prod 未打开,它可以完美运行,但无法告诉最终用户在执行宏之前确保没有打开任何连接窗口。

非常感谢

这是我的代码:

Sub my_sap
  Dim SapGui, Applic, connection, session, WSHShell

  strconnection = Worksheets("sap_info").Range("A2").Value
  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('LA-6-Prod', True)
  Set session = connection.Children(0)

'===============SAP SCRIPTING===========

'Execute SAP stuffs, I put in here what I recorded from sap record and it works OK

'=======================================

  Set session = Nothing
  connection.CloseSession ("ses[0]")
  Set connection = Nothing
  Set sap = Nothing

End Sub

【问题讨论】:

标签: excel vba scripting sap-gui


【解决方案1】:

你可以使用一个函数来测试连接是否已经打开,使用类似这样的东西......

Sub sap()

If SAP_Connection Then
MsgBox ("Sap is Open so just attached to session(0)")
Else
MsgBox ("Sap is NOT open so open Logon Window")
End If

End Sub

函数看起来像这样......

Function SAP_Connection() As Boolean

On Error GoTo ErrSap

If Not IsObject(SapApplication) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set SapApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = SapApplication.Children(0)
End If
If Not IsObject(Session) Then
   Set Session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject Session, "on"
   WScript.ConnectObject SapApplication, "on"
End If

SAP_Connection = True
Exit Function

ErrSap:
SAP_Connection = False

End Function

【讨论】:

    【解决方案2】:

    如果您在本地客户端安装中使用 SAP BAPI 功能,则可以绕过执行 shell 命令等的需要。

    VBA 中的一种方法是在模块中为 SAP 函数创建一个 Object 类型的全局变量。

    1. 在同一模块的一个子例程中,创建对象并登录到 SAP。
    2. 在其他子/函数中执行一些操作。
    3. 使用最终子例程注销并处置对象。

    全局变量在处理期间保持会话打开。

    例如,在模块顶部的全局变量中,您可以放置​​:

    Dim objR3 As Object  'SAP functions / connection
    

    假设您制作了一个 Access 表单,其中包含一些用于登录属性的文本字段(您也可以为此使用 Excel,并且只引用单元格)。然后,在您的登录子例程中,使用如下内容:

    strUser = frm!txtSAPUser
    strPwd = frm!txtPassword
    strClient = frm!txtClient
    strSystem = frm!txtSystem
    strServer = frm!txtServer
    Set objR3 = CreateObject("SAP.Functions")
    With objR3.Connection
        .System = strSystem
        .client = strClient
        .User = strUser
        .Password = strPwd
        .language = "EN"
        .ApplicationServer = strServer
        .SystemNumber = strSystem
        '--if no logon then exit
    If .logon(0, True) <> True Then
        MsgBox "Login failed.", vbExclamation, "Login"
    End If
    End With
    

    然后在其他子程序中做了一些动作后,你可以在你的注销子程序中使用注销命令:

        objR3.Connection.LOGOFF
    

    当您对 SAP 接口执行一些脚本时,此方法将使您的连接保持打开状态。

    【讨论】:

      【解决方案3】:

      我知道我对此有点晚了,但也许有人想知道如何与某个 sap 会话进行交互(也许这种方式有点复杂,但它有效)。因此,如果 SAP 已经打开并且您希望 SAP 打开另一个会话并与新会话交互,您需要知道创建了哪个会话号。以下是我获取号码​​的方式:

      Function SessNum()
      
      Dim obj_SapGuiAuto As Object
      Dim obj_Application As Object
      Dim obj_Connection As Object
      Dim obj_Session As Object
      Dim a As Integer, b As Integer, c As Integer, d As Integer, e As Integer
      Dim f As Integer, g As Integer, h As Integer, i As Integer, j As Integer
      Dim k As Integer
      
      Set obj_SapGuiAuto = GetObject("SAPGUI")
      Set obj_Application = obj_SapGuiAuto.GetScriptingEngine
      Set obj_Connection = obj_Application.Children(0)
      
      
          Set obj_Session = obj_Connection.Children
          For Each obj_Session In obj_Connection.Children
              a = InStr(obj_Session.ID, "ses[") + 4
              b = InStr(a, obj_Session.ID, "]") - a
              c = Mid(obj_Session.ID, a, b)
              d = d + c
      '        Debug.Print c
          Next obj_Session
      '    Debug.Print "..." & d & "..."
          e = obj_Connection.sessions.Count
      '    Debug.Print e
          If e < 7 Then
              For f = 0 To 6
                  If InStr(1, d, f) > 0 Then
                      Set obj_Session = obj_Connection.Children(CInt(f))
                      obj_Session.createsession
                      Do
                          Application.Wait (Now + TimeValue("0:00:01"))
                          If obj_Connection.sessions.Count > e Then Exit Do
                      Loop
                      Exit For
                  End If
              Next f
          Else
              MsgBox "Too many SAP sessions are open.", vbOKOnly, "Problem"
              Exit Function
          End If
          For Each obj_Session In obj_Connection.Children
              g = InStr(obj_Session.ID, "ses[") + 4
              h = InStr(g, obj_Session.ID, "]") - g
              i = Mid(obj_Session.ID, g, h)
              j = j + i
      '        Debug.Print i
          Next obj_Session
      '    Debug.Print "..." & j & "..."
      '    Debug.Print "opened session: " & j - d
          k = j - d
      
      MsgBox "created session: " & k, vbOKOnly, "SessionNumber"
      
      
      End Function
      

      我最多可以打开 7 个会话,这就是变量 f 不能大于 6 的原因(7 个会话意味着 0 到 6)。

      如果有帮助,反馈会很好。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多