简体   繁体   English

如何在 Excel 或 Visual Studio 中从 VBA 执行 SAP GUI 宏?

[英]How to execute SAP GUI Macro from VBA in Excel or Visual Studio?

I want to edit and launch SAP macros from either the VBA editor in Excel or from a Windows Form Application.我想从 Excel 中的 VBA 编辑器或 Windows 窗体应用程序编辑和启动 SAP 宏。

I cannot get my code to "connect" to the current SAP session when the script is used in a compiler.当脚本在编译器中使用时,我无法让我的代码“连接”到当前的 SAP 会话。 The only time it works is when I open the original VBScript that the macro recorder creates.它唯一有效的时间是当我打开宏记录器创建的原始 VBScript 时。

The way SAP is set up on my system we are automatically logged in to SAP Logon and launch a new sap GUI window via a web portal saplogon.com/irj/portal, I don't need to physically log on before I interact with the GUI. SAP 在我的系统上设置的方式我们会自动登录到 SAP Logon 并通过 Web 门户 saplogon.com/irj/portal 启动一个新的 sap GUI 窗口,在与图形用户界面。

When I copy the macro script verbatim I get runtime errors:当我逐字复制宏脚本时,出现运行时错误:

If Not IsObject(application) Then
   Set SapGuiAuto  = GetObject("SAPGUI")
   Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
   Set connection = application.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 application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/subSA_0100_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2410/subSA_2410_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2510/ctxtKUWEV-KUNNR").text = "I033"
session.findById("wnd[0]/usr/subSA_0100_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2410/subSA_2410_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2510/ctxtKUWEV-KUNNR").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]/usr/lbl[12,3]").setFocus
session.findById("wnd[1]/usr/lbl[12,3]").caretPosition = 16
session.findById("wnd[1]").sendVKey 2
session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").setCurrentCell 1,"MAKTX2"
session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").doubleClickCurrentCell
session.findById("wnd[1]/tbar[0]/btn[0]").press`

The way I have been trying to go about this is to add the sapfewselib reference into the project and then declare the SAP GUI object.我一直试图解决这个问题的方法是将 sapfewselib 引用添加到项目中,然后声明 SAP GUI 对象。

All the examples I can find are on how to log on to a new SAP session.我能找到的所有示例都是关于如何登录到新的 SAP 会话的。 I can't understand why the code will execute when a notepad is saved as a VBScript file but not when attached to a button in a Windows Form Application or an Excel Spreadsheet even though I have added the reference.我不明白为什么代码会在记事本保存为 VBScript 文件时执行,但在附加到 Windows 窗体应用程序或 Excel 电子表格中的按钮时不会执行,即使我已经添加了引用。

So I still can't find an example in c# which doesn't include log in but for the VBA editor in Excel the following works, but note in Visual Studio 'set' and 'let' keywords have been retired.所以我仍然无法在 c# 中找到一个不包括登录的示例,但对于 Excel 中的 VBA 编辑器,以下工作是有效的,但请注意 Visual Studio 中的“set”和“let”关键字已停用。 Just in case anyone else has the same issue, this is what you need to assign to a button:以防万一其他人有同样的问题,这就是您需要分配给按钮的内容:

    Sub Button1_Click()

    Dim SapGuiAuto
    Dim SetApp
    Dim Connection
    Dim Session

    Set SapGuiAuto = GetObject("SAPGUI")
    Set SetApp = SapGuiAuto.GetScriptingEngine
    Set Connection = SetApp.Children(0)
    Set Session = Connection.Children(0)

    Session.findById("wnd[0]").maximize

//Then your script to control gui below this://
    Session.findById("wnd[0]/usr/subSA_0100_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2410/subSA_2410_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2510/ctxtKUWEV-KUNNR").Text = "I033"
    Session.findById("wnd[0]/usr/subSA_0100_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2410/subSA_2410_1:SAPMZCX_CSDSLSBM5001_OFS_OTS:2510/ctxtKUWEV-KUNNR").caretPosition = 4
    Session.findById("wnd[0]").sendVKey 0
    Session.findById("wnd[1]/usr/lbl[12,3]").SetFocus
    Session.findById("wnd[1]/usr/lbl[12,3]").caretPosition = 16
    Session.findById("wnd[1]").sendVKey 2
    Session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").setCurrentCell 1, "MAKTX2"
    Session.findById("wnd[0]/shellcont/shell/shellcont[1]/shell").doubleClickCurrentCell
    Session.findById("wnd[1]/tbar[0]/btn[0]").press

    End Sub

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

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