How to automate SAP GUI using Excel VBA?

I am trying to log on to SAP. The Excel VBA code gives me a popup window confirming my information however when I submit the form it does not take me to a new SAP GUI window. Additionally is there a way to automate all the popup boxes asking for confirmation on my information? I want this code eventually to run at certain times of the day, and I might not be available to input any data.

Sub login1() Dim sap As Object Dim conn As Object Set sap = CreateObject("SAP.Functions") Set conn = sap.Connection conn.System = "System Test Environment" conn.client = "100" conn.user = "user" conn.Password = "password" conn.Language = "EN" If conn.logon(0, False) <> True Then MsgBox "Logon to the SAP system is not possible", vbOKOnly, "Comment" Else End If End Sub 
13.2k 5 5 gold badges 23 23 silver badges 54 54 bronze badges asked Nov 6, 2017 at 18:58 thePattiestOfKakes thePattiestOfKakes 19 1 1 silver badge 6 6 bronze badges Commented Nov 30, 2019 at 18:50

You are mentioning "SAP.Functions", which is an RFC component part of SAP GUI. RFC is a protocol for programs to communicate between two machines, not to automate the screens. What you need is SAP GUI Scripting.

Commented Jun 20, 2023 at 17:54

2 Answers 2

This Macro will never open a SAP Window - it will create an SAP-Object within VBA where you can work with SAP-RFC-Functions. (Reading Data from SAP, Writing Data into SAP)

In your version the SAP connection will be unaccessible after "End Sub". You have to declair the Object outside the sub.

This works silent (without dialog) for me:

Dim sap As Object Public Function login1() As Boolean Set sap = CreateObject("SAP.Functions") sap.Connection.System = "System Test Environment" sap.Connection.client = "100" sap.Connection.user = "user" sap.Connection.Password = "password" sap.Connection.Language = "EN" If sap.Connection.logon(0, False) <> True Then sap.RemoveAll MsgBox "Logon to the SAP system is not possible", vbOKOnly, "Comment" Else login1 = true End If End Function Public Function SAPLogoff() On Error Resume Next sap.RemoveAll sap.Connection.logoff LoggedOn = False Set sap = Nothing 'Set conn = Nothing End Function