Skip to Content
Former Member
Jun 22, 2015 at 10:56 AM

SAP Logon with VBA


Good morning, everybody!

I've been looking for the solution in the last days but I really have not managed to succeed:

I am trying to make a vba code to:

1-log into SAP,

2-run some transactions

3-export to excel.

But even the "log into SAP" part is not OK!

I tried several codes, the one below OPENS the SAP logon screen, but does not fill in any fields. I Used CreateObject("Sapgui.ScriptingCtrl.1"):

Sub Entrar_SAP()

If Not IsObject(SAPguiApp) Then

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

End If

If Not IsObject(Connection) Then

Set Connection = SAPguiApp.OpenConnection("xxxxxxx)", True)

End If

If Not IsObject(session) Then

Set session = Connection.Children(0)

End If

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

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

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

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

session.findById("wnd[0]/usr/txtRSYST-LANGU").SetFocus session.findById("wnd[0]/usr/txtRSYST-LANGU").caretPosition = 2

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

ANother one, with CreateObject("SAP.Functions"), showed:

"RFC error received. No RFC authorization for function module RFC PING"

the code is:


Dim objBAPIControl As Object 'Function Control (Collective object)

Dim sapConnection As Object 'Connection object

Set objBAPIControl = CreateObject("SAP.Functions")

Set sapConnection = objBAPIControl.Connection

sapConnection.Client = "xxxxx"

sapConnection.User = "xxxxxx"

sapConnection.Language = "PT"

sapConnection.hostname = "xxxxx"

sapConnection.Password = "xxxxxxxx" 'Fake password

sapConnection.SystemNumber = "4"

sapConnection.System = "xxxxxx)"


If sapConnection.Logon(1, True) <> True Then

MsgBox "No connection to R/3!"

Exit Sub 'End program

End If

Can someone please help me?