VBA 调用 SAP 事务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25130731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:05:28  来源:igfitidea点击:

VBA to call SAP transaction

excelvbasap

提问by alam

I am new to SAP and this site. I am trying to call a SAP transaction from Excel, I use many codes from the internet but still a failure. Doesn't matter what code I use, I always get same error message as

我是 SAP 和本网站的新手。我正在尝试从 Excel 调用 SAP 事务,我使用了互联网上的许多代码,但仍然失败。不管我使用什么代码,我总是收到相同的错误消息

A script is opening a connection to system SYSTEM

脚本正在打开与系统 SYSTEM 的连接

than error saying:

比错误说:

Runtime error 1000 Error description not avialable"

运行时错误 1000 错误描述不可用”

Than after debug it highlights below in yellow

比调试后它在下面以黄色突出显示

Set Connection = SapGuiApp.OpenConnection("SYSTEM", True)

its been doing this with all code I tried to pull this.

它一直在使用我试图提取的所有代码来执行此操作。

Code

代码

Dim sap As Object 
Dim conn As Object

Sub T_login()

    Set sap = CreateObject("SAP.Functions")
    Set conn = sap.Connection
    conn.System = "SYSTEM"
    conn.client = "603"
    conn.user = "LANAX001"
    conn.Password = "alamzeb4"
    conn.Language = "EN"

If conn.logon(0, False) Then

    MsgBox "Logon to the SAP system is not possible", vbOKOnly, "Comment"

Else

End If

If Not IsObject(SapGuiApp) Then

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

End If

If Not IsObject(Connection) Then

    Set Connection = SapGuiApp.OpenConnection("SYSTEM", True)

End If

If Not IsObject(session) Then

    Set session = Connection.Children(0)     
    'launch a transaction
    session.findById("wnd[0]").Maximize
    session.findById("wnd[0]/tbar[0]/okcd").Text = "FS10N"
    session.findById("wnd[0]").sendVKey 0

End Sub

回答by Ted

This is what I use to load data to SAP - verified to work. HTH.

这是我用来将数据加载到 SAP 的方法 - 经验证可以正常工作。哈。

If Not IsObject(App) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
   Set Connection = App.Children(0)
End If
If Not IsObject(session) Then
   Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject App, "on"
End If

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nXXXX" 'XXX = your t-code
... ...

Afterwards, it depends on what you do in the t-code.

之后,这取决于您在 t 代码中执行的操作。

Good luck!

祝你好运!