cancel
Showing results for 
Search instead for 
Did you mean: 

VBA SAP Session Problem

Former Member
0 Kudos

Hello Guys,

I am trying to implement some automations to my porcess using VBA, but I am having some issues when trying to execute SAP Script from my Excel Macro.

I am able to connect and log into SAP, but when I try to execute transaction I get following error:

Here is my code:


Sub sap_cn()

Dim app as Object

Dim cnn as Object

Dim ses as Object

If App Is Nothing then

     Set App = CreateObject("Sapgui.ScriptingCtrl.1")

End If

If cnn Is Nothing then

     Set cnn = app.OpenConnection("system", True)

End If

If ses Is Nothing then

     Set ses = cnn.Children(0)

End If

With ses

     .findById("wnd[0]/usr/txtRSYST-MANDT").Text = "mndt"

     .findById("wnd[0]/usr/txtRSYST-BNAME").Text = "usr"

     .findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "pass"

     .findById("wnd[0]/usr/txtRSYST-LANGU").Text = "PL"

End with

ses.findByID("wnd[0]/tbar[0]/okcd".Text = "ME21" ' it crushes in this line

Set ses = Nothing

Set cnn = Nothing

Set app = Nothing

End Sub

When I look into object inspector after Login into SAP can see that session object has no properties:

Any idea what do I wrong ?

Thanks in advance for help

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Looks like you've missed a close bracket on the statement


Session.findbyID("wnd[0]/tbar[0]/okcd").text="ME21"

I personally prefer Session as my session object so I can copy straight from script recording into my code prevents a lot of typo errors

Former Member
0 Kudos

I have corrected all typo's in my code and still get the follwoing error.

Any other idea ? Maybe references I should add to my VBA project ?

script_man
Active Contributor
0 Kudos

Hi Arek,

I suspect that this is a command line, which is missing:

...

End with

ses.findById("wnd[0]").sendVkey 0
...

Regards,

ScriptMan

Former Member
0 Kudos

I've added following line and still have the same problem.

stefan_schnell
Active Contributor
0 Kudos

Hello Arek,

your code looks good. With the object declarations it is not necessary to reference to any library.

I have try the following code in VBA debugger with success:

Sub sap_cn()
 
Dim app As Object
Dim cnn As Object
Dim ses As Object
 
If app Is Nothing Then
  Set app = CreateObject("Sapgui.ScriptingCtrl.1")
End If
 
If cnn Is Nothing Then
  Set cnn = app.OpenConnection("NSP Testsystem", True)
End If
 
If ses Is Nothing Then
  Set ses = cnn.Children(0)
End If

With ses
.FindById("wnd[0]/usr/txtRSYST-MANDT").Text = "001"
.FindById("wnd[0]/usr/txtRSYST-BNAME").Text = "BCUSER"
.FindById("wnd[0]/usr/pwdRSYST-BCODE").Text = "minisap"
.FindById("wnd[0]/usr/txtRSYST-LANGU").Text = "EN"
.FindById("wnd[0]").sendVkey 0
End With
 
ses.FindById("wnd[0]/tbar[0]/okcd").Text = "ME21"
ses.FindById("wnd[0]").sendVkey 0
 
cnn.CloseConnection
 
Set ses = Nothing
Set cnn = Nothing
Set app = Nothing
 
End Sub

Cheers

Stefan

Former Member
0 Kudos

I don't know why but after logging into SAP my session object "collapse" when I look into properties it has no Id or name.Can it be somehow linked with SAP itself ?

stefan_schnell
Active Contributor
0 Kudos

Hello Arek,

please look at TAC RZ11 with the profile parameter sapgui/user_scripting_per_user, if it is set to TRUE you need S_SCR authority for action execute.

Let us know your results.

Cheers

Stefan

Answers (0)