简体   繁体   English

如何使用 Excel VBA 访问 SAP?

[英]How to access SAP using Excel VBA?

I am trying to log on to SAP.我正在尝试登录 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. Excel VBA 代码给了我一个确认我的信息的弹出窗口,但是当我提交表单时,它不会将我带到一个新的 SAP 窗口。

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.此宏永远不会打开 SAP 窗口 - 它会在 VBA 中创建一个 SAP 对象,您可以在其中使用 SAP-RFC-Functions。 (Reading Data from SAP, Writing Data into SAP) (从 SAP 读取数据,将数据写入 SAP)

In your version the SAP connection will be unaccessible after "End Sub".在您的版本中,“End Sub”后将无法访问 SAP 连接。 You have to declair the Object outside the sub.您必须在 sub 之外声明 Object。

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!由于您想“打开一个新的 SAP 窗口”,您必须采取不同的方法!

At First try to open the new instance of SAP from the DOS Commandline with "sapshcut":首先尝试使用“sapshcut”从 DOS 命令行打开 SAP 的新实例:

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:如果您的 SystemName 没有空格(我相信如此!)那么您也可以这样写:

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:当此调用适用于您的凭据时,您可以像这样将其传输到 Excel:

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.您还可以通过向字符串添加“-command=your_tcode”来添加交易。

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""" (!)如果您的参数中有空格,并且您只能使用命令行中的 -system="System Test Environment" 启动它,则您必须在 Excel 中使用 -system="""System Test Environment""" (!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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