Skip to Content
avatar image
Former Member

SAP VBA Scripting two ECC systems

We recently went through an acquisition and are running 2 ECC systems one client is HBP and the other is SP3.

We are currently using

' RUNS SAP
'If Not IsObject(Application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Application1 = SapGuiAuto.GetScriptingEngine
'End If
If Not IsObject(Connection) Then
Set Connection = Application1.Children(0)
End If
If Not IsObject(session) Then
' ########################### you can change this 0 to 1, 2, 3, 4, or 5 to change SAP session ########
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject Application1, "on"
End If

But how can we set this up so we can tell it which client to look for?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Jan 30 at 07:30 PM

    Hello Stacy,

    you can find a solution, how to select a specific session, here.

    Let us know if it is solve your problem.

    Request: Could you please post your future SAP GUI Scripting questions in the SAP GUI Community. This is the correct section for this kind of questions, because SAP GUI Scripting is part of the SAP GUI Frontend. On this way, you will receive an answer to your questions faster. Thank you.

    Best regards
    Stefan

    Add comment
    10|10000 characters needed characters exceeded

    • Hello Stacy,

      you can exactly use the approach from the referenced post. E.g. like this (it is in VBScript but it should not be very difficult to transfer it to VBA):

        Function GetSystem(SID)
         
            Set SapGuiAuto = GetObject("SAPGUI")
            If Not IsObject(SapGuiAuto) Then
              Exit Function
            End If
      
            Set SapAppl = SapGuiAuto.GetScriptingEngine
            If Not IsObject(SapAppl) Then
              Exit Function
            End If
      
            Set CollCon = SapAppl.Connections()
            If Not IsObject(CollCon) Then
              Exit Sub
            End If
           
            '-Loop over connections-------------------------------------------
              For i = 0 To CollCon.Count() - 1
      
                Set oCon = SapAppl.Children(CLng(i))
                If Not IsObject(oCon) Then
                  Exit Sub
                End If 
      
                Set CollSes = oCon.Sessions()
                If Not IsObject(CollSes) Then
                  Exit Sub
                End If 
             
                '-Loop over sessions------------------------------------------
                  For j = 0 To CollSes.Count() - 1
             
                    Set oSes = oCon.Children(CLng(j))
                    If Not IsObject(oSes) Then
                      Exit Sub
                    End If 
      
                    If oSes.Busy() = vbFalse Then
               
                      Set oSesInf = oSes.Info()
                      If IsObject(oSesInf) Then
      
                          SID = oSesInf.SystemName()
      
                          If SID = "BAP" Then
      
                            GetSystem = oSes
      
                          End If
      
                      End If
                   
                    End If
      
                  Next
      
              Next
      
          End Function

      All you have to do now is to call

      Session = GetSytem("BAP") 'Here you must insert the SID of your B-SAP
      If Not IsObject(Session) Then
        "Here you must insert your code to open a new session of your B-SAP
      End If

      Cheers
      Stefan