cancel
Showing results for 
Search instead for 
Did you mean: 

Open New Sessions and attach

Former Member
0 Kudos

Hello,

I am trying to figure out how while attacehd to SAP from VBA excel to open a new session and continue the task in the new session. The end goal is to run 3 different transactions and have them up each in a seperate window. this is what I have so far but the transaction doesn't continue in the new session created it defaults back to the original window.

Set SapGuiAuto = GetObject("SAPGUI")

Set SAP = SapGuiAuto.GetScriptingEngine

With SAP.Connections(0).Sessions(0)

.createsession

End With

Set SapGuiAuto = GetObject("SAPGUI")

Set SAP = SapGuiAuto.GetScriptingEngine

SAP.Connections(0).Sessions(0).StartTransaction (Tcode)

With SAP.Connections(0).Sessions(0)

.findById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-MATNR").Text = Selection

.findById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-WERKS").Text = "CHEM"

.findById("wnd[0]/tbar[0]/btn[0]").press

.findById("wnd[0]/tbar[1]/btn[34]").press

.findById("wnd[0]/tbar[1]/btn[35]").press

.findById("wnd[0]/tbar[1]/btn[36]").press

I am attaching once...creating a new session and attaching again but it does not seem to happen in the new window created....any help please?

Accepted Solutions (0)

Answers (1)

Answers (1)

script_man
Active Contributor
0 Kudos

Hi SLobo32, welcome to the forum !

The question is simple but I needed for the answer a lot of time. I have put my observations into a theory as folllows.

By the command "create session", we opened a new SAP session. The system decides which is given session number. The task is then, how is the new session number.


Session_number_max = 6                                  'the maximum number of possible SAP sessions
Dim session_number_(Session_number_max)

Set SapGuiAuto = GetObject("SAPGUI")
Set SAP = SapGuiAuto.GetScriptingEngine
Set connection = SAP.Children(0)
Set session    = connection.Children(0)

'------------------------------------------- new session connect -------------------------------------------------------------
session_number_all = connection.children.count - 1

for i = 1 to Session_number_max 
     session_number_(i) = 0
next  
    
for session_number = 0 to session_number_all
     Set session    = connection.Children(int(session_number))
     session_number_(session.info.sessionnumber) = session.info.sessionnumber
next

if session_number_all < Session_number_max - 1 then
     session.createsession
     do 
      wscript.sleep 500
      if connection.children.count - session_number_all >= 2 then exit do
     loop
     on error resume next
     Error_number = 1 
     for session_number = 0 to session_number_all + 1
          err.clear
          Set session    = connection.Children(int(session_number))
          if err.number > 0 or err.number < 0 then exit for
      
         if session_number_(session.info.sessionnumber) = 0 then 
            Error_number = 0
            exit for
         end if 
         'session.findById("wnd[0]").iconify                          'So you can send another SAP session in the task bar.
     next
     on error goto 0
else
 msgbox "New session is not possible."
end if
'------------------------------------------- new session connect -------------------------------------------------------------

If Error_number = 0 then

session.StartTransaction (Tcode)

With session
.findById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-MATNR").Text = Selection
.findById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-WERKS").Text = "CHEM"
.findById("wnd[0]/tbar[0]/btn[0]").press
.findById("wnd[0]/tbar[1]/btn34").press
.findById("wnd[0]/tbar[1]/btn35").press
.findById("wnd[0]/tbar[1]/btn36").press

. . . 

Regards,

ScriptMan

Edited by: ScriptMan on May 11, 2011 3:08 PM