control sap with vba

wizz

New Member
Joined
May 13, 2010
Messages
17
Hi,

I want to open sap and launch sap transaction using vba from excel.
Until now I managed only to logon, but i can't open a sap session and launch a transaction. Please, give me some ideeas. Here is the code until now:

Dim sapConn As Object 'Declare connection object
Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object
'Specify user
sapConn.Connection.user = "xxxxx"
'Then password
sapConn.Connection.Password = "xxxxxx"
'Client
sapConn.Connection.client = "600"
'Target server address
sapConn.Connection.ApplicationServer = "172.19.64.5"
'Language code
sapConn.Connection.Language = "EN"

If sapConn.Connection.logon(0, True) <> True Then
MsgBox "Cannot Log on to SAP" 'Issue message if cannot logon
Else
MsgBox "Logged on to SAP!"


thank you for your time!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi,
I found 2 methods to launch sap transaction using vba.
1. I have already a SAP session opened and using vba I just run transactions.
2. I open using vba a SAP session entering username and password and than the transactions.

Below I will explain both methods:

1.
'setting the connection with sap:
Dim App, Connection, session As Object
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
Set Connection = App.Children(0)
Set session = Connection.Children(0)

'launch a transaction
session.findById("wnd[0]").Maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "FS10N"
session.findById("wnd[0]").sendVKey 0

2.

Set app = CreateObject("Sapgui.ScriptingCtrl.1")
Set Connection = app.OpenConnection("name of the sap session in saplogon", True)
Set Session = Connection.Children(0)
'here is what you enter when you connect manualy:
Session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "client/mandant"
Session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = YourUserName
Session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = YourPassword
Session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "language"
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]").Maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "FS10N"
session.findById("wnd[0]").sendVKey 0

I hope I was usefull.
 
Upvote 0
Doesn't work :(
Is there anything else I could do?
I would really love to have method#1 work


You need to create a connection with SAP first via:

Code:
If IsObject(SAPapplication) Then
On Error Resume Next
    Set SapGuiAuto = GetObject("SAPGUI")
    If Err.Number <> 0 Then Exit Function
    Set SAPapplication = SapGuiAuto.GetScriptingEngine
    If Err.Number <> 0 Then Exit Function
    On Error GoTo 0
End If
If IsObject(Connection) Then
On Error Resume Next
    Set Connection = SAPapplication.Children(0)
    If Err.Number <> 0 Then Exit Function
End If


If IsObject(Session) Then
    Set Session = Connection.Children(0)
End If
SAPlogon = True


And then move forward with the actual process using whatever your transaction code is...

Code:
Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "VA03"
Session.findById("wnd[0]/tbar[0]/btn[0]").press


Let me know if you have any questions
 
Upvote 0
Hello Wizz, I could use your VBA code for open a SAP transaction from Excel but I would like to execute this transacction with filed values from excel, I mean say You can automatically load the values of this excel fields to execute SAP transaction?. I hope You can help me.
 
Upvote 0
Hello Wizz, I could use your VBA code for open a SAP transaction from Excel but I would like to execute this transacction with filed values from excel, I mean say You can automatically load the values of this excel fields to execute SAP transaction?. I hope You can help me.

Hello. Did you find how to put values from excel?
 
Upvote 0
Make sure you have the SAP reference enabled for these commands to work. Found here

Developer tab=>Visual Basic=>Tools=>References=> enable "SAP GUI Scripting API"
 
Upvote 0
Hello Wizz, I could use your VBA code for open a SAP transaction from Excel but I would like to execute this transacction with filed values from excel, I mean say You can automatically load the values of this excel fields to execute SAP transaction?. I hope You can help me.

Hello. Did you find how to put values from excel?

Below is what I do. Just change the name of the sheet and Cell coordinates you want to pull from.

Code:
Set session = GetObject("SAPGUI").GetScriptingEngine.Children(0).Children(0)

session.findById("wnd[0]/tbar[0]/okcd").Text = Worksheets("Sheet1").Cells(1, 1).Value
session.findById("wnd[0]").sendVKey 0
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top