Excel VBA从SAP NetWeaver中提取数据

如何使用VBAmacros将数据从SAP NetWeaver导入Excel?

通过SAP GUI,我可以通过几个不同的事务访问SAP系统中的数据。 我需要每天提取这些数据,然后将其格式化为Excel电子表格和报告。 手动提取这些数据非常耗时且容易出错。

我没有开发人员或系统支持。 我必须能够自己做一个非特权用户。

这是从我的答案复制在这里: https : //stackoverflow.com/a/19456656/2250183

这完全取决于您对SAP系统的访问权限。 导出数据和/或您的macros可以调用以直接获取数据或使SAP创build文件的RFC的ABAP程序可能是最好的。

然而,作为一般规则,寻找这种答案的人正在寻找一个即时的解决scheme,不需要他们的IT部门花费几个月来定制他们的SAP系统。

在这种情况下,您可能需要使用SAP GUI脚本。 SAP GUI脚本允许您以与自动化Excel相同的方式自动执行Windows SAP GUI。 事实上,您可以直接从Excelmacros调用SAP GUI。 在这里阅读更多。 SAP GUI具有与Excel类似的macros观录制工具。 它在VBScript中loggingmacros,与Excel VBA几乎相同,通常可以直接复制并粘贴到Excelmacros中。

示例代码

这是一个基于我可以访问的SAP系统的简单示例。

Public Sub SimpleSAPExport() Set SapGuiAuto = GetObject("SAPGUI") 'Get the SAP GUI Scripting object Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected Set session = SAPCon.Children(0) 'Get the first session (window) on that connection 'Start the transaction to view a table session.StartTransaction "SE16" 'Select table T001 session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").Text = "T001" session.findById("wnd[0]/tbar[1]/btn[7]").Press 'Set our selection criteria session.findById("wnd[0]/usr/txtMAX_SEL").text = "2" session.findById("wnd[0]/tbar[1]/btn[8]").press 'Click the export to file button session.findById("wnd[0]/tbar[1]/btn[45]").press 'Choose the export format session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").select session.findById("wnd[1]/tbar[0]/btn[0]").press 'Choose the export filename session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "test.txt" session.findById("wnd[1]/usr/ctxtDY_PATH").text = "C:\Temp\" 'Export the file session.findById("wnd[1]/tbar[0]/btn[0]").press End Sub 

脚本logging

为了帮助查找诸如wnd[1]/tbar[0]/btn[0]等元素的名称,可以使用脚本logging。 点击自定义本地布局button,它可能看起来有点像这样: 自定义本地布局
然后find脚本录制和回放菜单项。
脚本录制和播放
在此Morebutton,您可以查看/更改VB脚本logging到的文件。 输出格式有点乱,它会loggingselect文本,点击文本框等内容。

编辑:早期和晚期的约束力

提供的脚本应该直接复制到VBAmacros中。 它使用后期绑定, Set SapGuiAuto = GetObject("SAPGUI")定义了SapGuiAuto对象。

但是,如果要使用早期绑定,以便VBA编辑器可以显示所用对象的属性和方法,则需要在SAP GUI安装文件夹中添加对sapfewse.ocx的引用。