cancel
Showing results for 
Search instead for 
Did you mean: 

SAP VBA Scripting two ECC systems

former_member546423
Discoverer
0 Kudos

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?

Accepted Solutions (0)

Answers (1)

Answers (1)

stefan_schnell
Active Contributor
0 Kudos

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

former_member546423
Discoverer
0 Kudos

Sorry new to questions.... So I have a script in excel that open SAP and runs COOIS one for our B-SAP system and the other for our C-SAP system and I'm trying to automate it to do both. I have a .bat file that runs the macros but I need the macro that is supposed to run for the B-SAP system to look for that session that is open.

Here is my script currently

Sub Auto_Open()
Call Module1.MainMacro
End Sub
Sub MainMacro()
'
' Macro1 Macro

Application.DisplayAlerts = False
Application.ScreenUpdating = False

Workbooks.Open Filename:= _
"Z:\xxxxxxx\yyyyyyy\4COOIS.csv"
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy

If Not IsObject(Application1) 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
Set Session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject Session, "on"
WScript.ConnectObject Application1, "on"
End If
With Session
.findById("wnd[0]/tbar[0]/okcd").Text = "/ncoois"
.findById("wnd[0]").sendVKey 0
.findById("wnd[0]/usr/ssub%_SUBSCREEN_TOPBLOCK:PPIO_ENTRY:1100/ctxtPPIO_ENTRY_SC1100-ALV_VARIANT").Text = "/M&I_OPNRES"
.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/btn%_S_AUFNR_%_APP_%-VALU_PUSH").press
.findById("wnd[1]/tbar[0]/btn[24]").press
.findById("wnd[1]/tbar[0]/btn[8]").press
.findById("wnd[0]/tbar[1]/btn[8]").press
.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").pressToolbarButton "&NAVIGATION_PROFILE_TOOLBAR_EXPAND"
.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").selectContextMenuItem "&PC"
.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select
.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").SetFocus
.findById("wnd[1]/tbar[0]/btn[0]").press
.findById("wnd[1]/usr/ctxtDY_PATH").Text = "Z:\Information Flow\Open Order Reservations"
.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "COOIS.txt"
.findById("wnd[1]/tbar[0]/btn[11]").press
End With
Application.Quit
End Sub

stefan_schnell
Active Contributor
0 Kudos

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