简体   繁体   English

Excel VBA从SAP NetWeaver提取数据

[英]Excel VBA pulling data from SAP NetWeaver

How do you pull data from SAP NetWeaver into Excel using a VBA macro? 如何使用VBA宏将数据从SAP NetWeaver提取到Excel中?

I have data in an SAP system accessible in a few different transactions through the SAP GUI. 我在SAP系统中有数据,可以通过SAP GUI通过一些不同的事务进行访问。 I need to extract this data on a daily basis, then format it into Excel spreadsheets and reports. 我需要每天提取这些数据,然后将其格式化为Excel电子表格和报告。 Manually extracting this data is very time consuming and error prone. 手动提取此数据非常耗时且容易出错。

I don't have developer or system support. 我没有开发人员或系统支持。 I have to be able to do it myself as an unprivileged user. 我必须能够以非特权用户身份自己进行操作。

This is copied from my answer here: https://stackoverflow.com/a/19456656/2250183 这是从我的答案中复制过来的: https : //stackoverflow.com/a/19456656/2250183

This all depends on what sort of access you have to your SAP system. 这完全取决于您对SAP系统的访问权限。 An ABAP program that exports the data and/or an RFC that your macro can call to directly get the data or have SAP create the file is probably best. 导出数据的ABAP程序和/或宏可以调用以直接获取数据或由SAP创建文件的RFC可能是最好的。

However as a general rule people looking for this sort of answer are looking for an immediate solution that does not require their IT department to spend months customizing their SAP system. 但是 ,通常来说,寻找这种答案的人们正在寻找一种即时解决方案,该解决方案不需要IT部门花费数月的时间来定制SAP系统。

In that case you probably want to use SAP GUI Scripting. 在这种情况下,您可能要使用SAP GUI脚本。 SAP GUI scripting allows you to automate the Windows SAP GUI in much the same way as you automate Excel. SAP GUI脚本允许您以与自动化Excel几乎相同的方式来自动化Windows SAP GUI。 In fact you can call the SAP GUI directly from an Excel macro. 实际上,您可以直接从Excel宏调用SAP GUI。 Read up more on it here . 这里阅读更多内容。 The SAP GUI has a macro recording tool much like Excel does. SAP GUI具有类似于Excel的宏记录工具。 It records macros in VBScript which is nearly identical to Excel VBA and can usually be copied and pasted into an Excel macro directly. 它在VBScript中记录了与Excel VBA几乎相同的宏,通常可以直接将其复制并粘贴到Excel宏中。

Example Code 范例程式码

Here is a simple example based on a SAP system I have access to. 这是一个基于我可以访问的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

Script Recording 脚本录制

To help find the names of elements such as wnd[1]/tbar[0]/btn[0] you can use script recording. 为了帮助查找诸如wnd[1]/tbar[0]/btn[0]之类的元素的名称,您可以使用脚本记录。 Click the customize local layout button, it probably looks a bit like this: 单击自定义本地布局按钮,它可能看起来像这样: 自定义本地布局
Then find the Script Recording and Playback menu item. 然后找到“脚本记录和播放”菜单项。
脚本录制和播放
Within that the More button allows you to see/change the file that the VB Script is recorded to. 在“ More按钮中,您可以查看/更改VB脚本记录到的文件。 The output format is a bit messy, it records things like selecting text, clicking inside a text field, etc. 输出格式有点混乱,它记录诸如选择文本,在文本字段内单击等内容。

Edit: Early and Late binding 编辑:早期和晚期绑定

The provided script should work if copied directly into a VBA macro. 如果直接复制到VBA宏中,提供的脚本应该可以工作。 It uses late binding, the line Set SapGuiAuto = GetObject("SAPGUI") defines the SapGuiAuto object. 它使用后期绑定, Set SapGuiAuto = GetObject("SAPGUI")定义了SapGuiAuto对象。

If however you want to use early binding so that your VBA editor might show the properties and methods of the objects you are using, you need to add a reference to sapfewse.ocx in the SAP GUI installation folder. 但是,如果要使用早期绑定,以便VBA编辑器可以显示所使用对象的属性和方法,则需要在SAP GUI安装文件夹中添加对sapfewse.ocx的引用。

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

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