1

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
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • You can find your answer here https://stackoverflow.com/a/58348742/11636588 or here https://simpleexcelvba.com/connect-to-sap-via-excel-vba/ – Teamothy Nov 30 '19 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. – Sandra Rossi Jun 20 '23 at 17:54

2 Answers2

1

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
  • thanks for the input, unfortunately the problem of an additional popup box persists. Also an instance of the sap interface doesn't open up, is there anything you know that might explain why? – thePattiestOfKakes Nov 13 '17 at 14:11
  • As I wrote, this kind of makro will never open a "sap interface" - it is for working with an sap-object inside vba. What you want is another thing! I´ll write you a second solution which opens a SAP Interface out of VBA in an seperate answer. – Friedrich Kölbel Nov 14 '17 at 22:08
1

Since you want to "open a new SAP Window" you have to make a different approach!

At First try to open the new instance of SAP from the DOS Commandline with "sapshcut":

C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe -system="System Test Environment" -client="100" -user="user" -password="password" -language="EN"

If your SystemName has no Spaces (and I belive so!) then you can write it also like:

C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe -system=SystemTestEnvironment -client=100 -user=user -password=password -language=EN

When this call works with your credentials, then you can transfer it to Excel like this:

Sub login1()

  Call Shell("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapshcut.exe -system=SystemTestEnvironment -client=100 -user=user -password=password -language=EN",vbNormalFocus)

End Sub

You could also add a transaction by adding "-command=your_tcode" to the String.

If your have spaces in the parameters and you could only start it with -system="System Test Environment" from the Commanline, you will have to escape this in Excel with -system="""System Test Environment""" (!)