简体   繁体   中英

How to access SAP 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 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

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

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""" (!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM