on 02-12-2016 8:45 AM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.