cancel
Showing results for 
Search instead for 
Did you mean: 

Visual Basic and SAP scripting GUI

Former Member

I'm working to automatize SAP program using VBA.

I record some code using SAP scripting. Now I can open six sessions ( SAP windows ) and in each of them put some data.

I have a problem to execute these sessions parallel ( run all session in one time ). Can you help me ?

Thanks, Luc

Accepted Solutions (1)

Accepted Solutions (1)

holger_khn
Contributor

Hello.

Running all sessions parallel controlled by EXCEL VBA coding is only possible by running seperate Excel instances parallel.

One Excel template can´t control multiple SAP sessions.

One Excel instance used by several templates can´t control more than one OLE-Connection to SAP session.

Each Excel instance can handle an single OLE-Connection to SAP session.

Just open a new Excel instance and load template by open-Dialog box from that instance.

Best regards,

Holger

Former Member
0 Kudos

Thank you but I still don't know how to do this. Can you clarify ?

There is no option to run separate macro parallel.

script_man
Active Contributor
0 Kudos

Hi Holger,

such extreme situations in SAP GUI Scripting inspire me again and again. You are absolutely right with what you say. But you can outsmart Excel with Windows Scripting Host.

For example as follows:

Sub Parallel_Sessions()

Set SapGuiAuto = GetObject("SAPGUI")

Set SAP_Application = SapGuiAuto.GetScriptingEngine

Set Connection = SAP_Application.Children(0)

Set session = Connection.Children(0)

Set wshell = CreateObject("Wscript.Shell")

Time_1 = TimeValue(Time)

For mysession = 0 To 5

Set session = Connection.Children(Int(mysession))

session.findById("wnd[0]").maximize

session.findById("wnd[0]/tbar[0]/okcd").Text = "/nreiscn"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/ctxtS_BUKRS-LOW").Text = "2000"

session.findById("wnd[0]/usr/chkP_USEPTN").SetFocus

session.findById("wnd[0]/usr/chkP_USEPTN").Selected = True

session.findById("wnd[0]/usr/chkP_GRID").Selected = True

session.findById("wnd[0]/usr/chkP_DSPBUP").Selected = True

session.findById("wnd[0]/usr/ctxtSO_PARTN-LOW").Text = "1"

session.findById("wnd[0]/usr/ctxtSO_PARTN-HIGH").Text = "1000"

session.findById("wnd[0]/usr/chkP_DSPBUP").SetFocus

If mysession < 5 Then session.createSession

'wscript.sleep 2000

waitTill = Now() + TimeValue("00:00:02")

While Now() < waitTill

   DoEvents

Wend

 

If mysession = 5 Then

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

Else

    wshell.Run """c:\tmp\Script_parallel_Sessions_1.vbs""" & " " & mysession

End If

Next

Time_2 = TimeValue(Time)

Time_0 = Round((Time_2 - Time_1) * 24 * 3600, 2)

MsgBox "This run for 6 sessions lasted " & Time_0 & " s.", vbInformation + vbMsgBoxForeground, "Note"

End Sub

Script_parallel_Sessions_1.vbs in c:\tmp:

If Not IsObject(application) Then

   Set SapGuiAuto  = GetObject("SAPGUI")

   Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

   Set connection = application.Children(0)

End If

for mysession  = 0 to 5

    if cstr(mysession) = cstr(wscript.arguments(0)) then

       Set session    = connection.Children(int(mysession))

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

      

    end if

next

The execution in this way lasted on my computer about 35 s.

The following example of a VBA - program lasted on my computer about 102 s.

Sub Seriell_Sessions()

Set SapGuiAuto = GetObject("SAPGUI")

Set SAP_Application = SapGuiAuto.GetScriptingEngine

Set Connection = SAP_Application.Children(0)

Set session = Connection.Children(0)

Set wshell = CreateObject("Wscript.Shell")

Time_1 = TimeValue(Time)

For mysession = 0 To 5

Set session = Connection.Children(Int(mysession))

session.findById("wnd[0]").maximize

session.findById("wnd[0]/tbar[0]/okcd").Text = "/nreiscn"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/ctxtS_BUKRS-LOW").Text = "2000"

session.findById("wnd[0]/usr/chkP_USEPTN").SetFocus

session.findById("wnd[0]/usr/chkP_USEPTN").Selected = True

session.findById("wnd[0]/usr/chkP_GRID").Selected = True

session.findById("wnd[0]/usr/chkP_DSPBUP").Selected = True

session.findById("wnd[0]/usr/ctxtSO_PARTN-LOW").Text = "1"

session.findById("wnd[0]/usr/ctxtSO_PARTN-HIGH").Text = "1000"

session.findById("wnd[0]/usr/chkP_DSPBUP").SetFocus

If mysession < 5 Then session.createSession

'wscript.sleep 2000

waitTill = Now() + TimeValue("00:00:02")

While Now() < waitTill

   DoEvents

Wend

 

'If mysession = 5 Then

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

'Else

'   wshell.Run """c:\tmp\Script_parallel_Sessions_1.vbs""" & " " & mysession

'End If

Next

Time_2 = TimeValue(Time)

Time_0 = Round((Time_2 - Time_1) * 24 * 3600, 2)

MsgBox "This run for 6 sessions lasted " & Time_0 & " s.", vbInformation + vbMsgBoxForeground, "Note"

End Sub

Regards,

ScriptMan

Former Member
0 Kudos

Hi,

Why I get an error msg?

During run Script_parallel_Sessions_1.vbs I have

---------------------------

Windows Script Host

---------------------------

Script: C:\Users\PH323111\Desktop\Script_parallel_Sessions_1.vbs

Line: 9

Char: 5

Error: Subscript out of range

Code: 800A0009

Source: Microsoft VBScript runtime error

---------------------------

OK  

---------------------------

Thank you for reply.

script_man
Active Contributor
0 Kudos

Hi,

it suggests that the parameter "mysession" was not passed correctly. The corresponding command line would look like this in your VBA-program:

wshell.Run """C:\Users\PH323111\Desktop\Script_parallel_Sessions_1.vbs""" & " " & mysession


explanation:


"""      3 x sign = "

" "      sign= " + 1 space + sign = "


Regards,

ScriptMan

Former Member
0 Kudos

Hi Script Man,

Thank you for reply. I changed path before your answer but it's still doesn't work. I think that is something  wrong with :

if cstr(mysession) = cstr(wscript.arguments(0)) then

What mean bold line?

Thanks,

Luc

script_man
Active Contributor
0 Kudos

Hi Luc,

wscript.arguments(0) contains the content of mysession in the following command:


wshell.Run """C:\Users\PH323111\Desktop\Script_parallel_Sessions_1.vbs"""   &   " "   &  mysession



You can try the following:


Sub Parallel_Sessions()

...

msgbox mysession

wshell.Run """C:\Users\PH323111\Desktop\Script_parallel_Sessions_1.vbs"""   &   " "   &  mysession

...

End Sub


Script_parallel_Sessions_1.vbs:


...

msgbox wscript.arguments(0)

if cstr(mysession) = cstr(wscript.arguments(0)) then

...


Regards,

ScriptMan




Former Member
0 Kudos

Done

Thank you gents.

@Script Man
@Holger Kohn

Former Member
0 Kudos

I have been running VB scripts for simple tasks in SAP.  today when I log into SAP via VPN my script errors out at the following step

Set Connection = SAPApplication.Children(0)

Set Session = Connection.Children(0)

If I run the script when directly working from the network then it is fine.  if gives an error only when working via VPN any help is appreciated

Former Member
0 Kudos

Maybe try this one:

If Not IsObject(app) Then

   Set SapGuiAuto = GetObject("SAPGUI")

   Set app = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(Connection) Then

   Set Connection = app.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 app, "on"

End If

Thanks.

Former Member
0 Kudos

Luc, i replaced my code with this.  in this case the script goes thru the bold lines below,

hence the session and app are not set.  so fails when it hits the transaction recording statement

If Not IsObject(app) Then

   Set SapGuiAuto = GetObject("SAPGUI")

   Set app = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(Connection) Then

   Set Connection = app.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 app, "on"

End If

......

....

Fails at line below

session.findById("wnd[0]/tbar[0]/okcd").Text = "/nsm30"

As i mentioned this works fine when i am connected directly in the network.  Fails only when i connect to SAP via VPN

help is appreciated

Former Member
0 Kudos

Friends really need help here

this is the code i have

Sub StartTransaction(SapGuiAuto, Connection, session)

' Dim SapGuiAuto As Object

Dim i As Long

i = 1

' There may be bad entries in the ROT from previous crashes

While i < 10 And SapGuiAuto Is Nothing

   i = i + 1

   On Error Resume Next

   Set SapGuiAuto = GetObject("SAPGUI")

   On Error GoTo 0

Wend

If SapGuiAuto Is Nothing Then

   MsgBox "Could not connect to SAPlogon process. Did you start it?", vbOKOnly Or vbCritical

   End

End If

On Error Resume Next

Set SAPApplication = SapGuiAuto.GetScriptingEngine

Set SapGuiAuto = Nothing

On Error GoTo 0

If SAPApplication Is Nothing Then

   MsgBox "Could not access GuiApplication. Maybe Scripting is disabled?", vbOKOnly Or vbCritical

   End

End If

Set Connection = SAPApplication.Children(0)

Set session = Connection.Children(0)

End Sub

Even though i have multiple sap sessions open, it fails at

Set session = Connection.Children(0)


Any help is appreciated


holger_khn
Contributor
0 Kudos

Hello.

You should start your own thread. Please include as well errormessage there. Thanks.

Best regards

Holger

Answers (2)

Answers (2)

0 Kudos

hi

how can I put sap remarks to excel please help its urgent my number is +919790971275

script_man
Active Contributor
0 Kudos

Hi Luc,

the following example might help you :

Script_parallel_sessions.vbs:

If Not IsObject(application) Then

   Set SapGuiAuto  = GetObject("SAPGUI")

   Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

   Set connection = application.Children(0)

End If

If Not IsObject(session) Then

   Set session    = connection.Children(0)

End If

for mysession = 0 to 5

set session = connection.Children(int(mysession))

session.findById("wnd[0]").maximize

session.findById("wnd[0]/tbar[0]/okcd").text = "/nreiscn"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/ctxtS_BUKRS-LOW").text = "2000"

session.findById("wnd[0]/usr/chkP_USEPTN").setFocus

session.findById("wnd[0]/usr/chkP_USEPTN").selected = true

session.findById("wnd[0]/usr/chkP_GRID").selected = true

session.findById("wnd[0]/usr/chkP_DSPBUP").selected = true

session.findById("wnd[0]/usr/ctxtSO_PARTN-LOW").text = "1"

session.findById("wnd[0]/usr/ctxtSO_PARTN-HIGH").text = "1000"

session.findById("wnd[0]/usr/chkP_DSPBUP").setFocus

if mysession < 5 then session.createSession

wscript.sleep 2000

'session.findById("wnd[0]/tbar[1]/btn[8]").press

'msgbox "Session: " & mysession & vbcr & "Time: " & time ,  vbInformation + vbMsgBoxForeground  , "Note"

set wshell = createobject("Wscript.Shell")

wshell.run """c:\tmp\Script_parallel_Sessions_1.vbs""" & " " & mysession

next

Script_parallel_sessions_1.vbs:

If Not IsObject(application) Then

   Set SapGuiAuto  = GetObject("SAPGUI")

   Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

   Set connection = application.Children(0)

End If

for mysession  = 0 to 5

    if cstr(mysession) = cstr(wscript.arguments(0)) then

       Set session    = connection.Children(int(mysession))

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

     

       msgbox "Session: " & mysession & vbcr & "Time: " & time ,  vbInformation + vbMsgBoxForeground , "Note"

    end if

next


Script_seriell_sessions.vbs:

If Not IsObject(application) Then

   Set SapGuiAuto  = GetObject("SAPGUI")

   Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

   Set connection = application.Children(0)

End If

If Not IsObject(session) Then

   Set session    = connection.Children(0)

End If

for mysession = 0 to 5

set session = connection.Children(int(mysession))

session.findById("wnd[0]").maximize

session.findById("wnd[0]/tbar[0]/okcd").text = "/nreiscn"

session.findById("wnd[0]").sendVKey 0

session.findById("wnd[0]/usr/ctxtS_BUKRS-LOW").text = "2000"

session.findById("wnd[0]/usr/chkP_USEPTN").setFocus

session.findById("wnd[0]/usr/chkP_USEPTN").selected = true

session.findById("wnd[0]/usr/chkP_GRID").selected = true

session.findById("wnd[0]/usr/chkP_DSPBUP").selected = true

session.findById("wnd[0]/usr/ctxtSO_PARTN-LOW").text = "1"

session.findById("wnd[0]/usr/ctxtSO_PARTN-HIGH").text = "1000"

session.findById("wnd[0]/usr/chkP_DSPBUP").setFocus

if mysession < 5 then session.createSession

wscript.sleep 2000

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

msgbox "Session: " & mysession & vbcr & "Time: " & time ,  vbInformation + vbMsgBoxForeground , "Note"

'set wshell = createobject("Wscript.Shell")

'wshell.run """c:\tmp\Script_parallel_Sessions_1.vbs""" & " " & mysession

next

But you can look for other suggestions also at this link:

Regards,

ScriptMan