Excel VBA pulling data from SAP NetWeaver

后端 未结 1 1665
半阙折子戏
半阙折子戏 2021-01-17 02:42

How do you pull data from SAP NetWeaver into Excel using a VBA macro?

I have data in an SAP system accessible in a few different transactions through the SAP GUI. I

相关标签:
1条回答
  • 2021-01-17 03:12

    This is copied from my answer here: https://stackoverflow.com/a/19456656/2250183

    This all depends on what sort of access you have to your SAP system. 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.

    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.

    In that case you probably want to use SAP GUI Scripting. SAP GUI scripting allows you to automate the Windows SAP GUI in much the same way as you automate Excel. In fact you can call the SAP GUI directly from an Excel macro. Read up more on it here. The SAP GUI has a macro recording tool much like Excel does. It records macros in VBScript which is nearly identical to Excel VBA and can usually be copied and pasted into an Excel macro directly.

    Example Code

    Here is a simple example based on a SAP system I have access to.

    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 aswnd[1]/tbar[0]/btn[0] you can use script recording. Click the customize local layout button, it probably looks a bit like this: Customize Local Layout
    Then find the Script Recording and Playback menu item.
    Script Recording and Playback
    Within that the More button allows you to see/change the file that the VB Script is recorded to. 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. It uses late binding, the line Set SapGuiAuto = GetObject("SAPGUI") defines the SapGuiAuto object.

    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.

    0 讨论(0)
提交回复
热议问题