Pulling data from SAP using Excel Macros

adastra picture adastra · Jun 19, 2014 · Viewed 25.9k times · Source

So I am trying to pull data from SAP using excel macros. I am new to VBA so please bear with me. I found a topic on here called VBA pulling data from SAP for dummies and I am confused. What I am trying to do is as follows:

  1. Copy a notification number from a list in excel.
  2. Go to the appropriate screen in SAP and paste this number in the search box.
  3. Open the long text box.
  4. Copy the long text.
  5. Paste into excel.

Here is the link VBA pulling data from SAP for dummies

I can't seem to get by Set session = connection.Children(0) 'Get the first session (window) on that connection.

Any help is much appreciated. The reason I am doing this is because SAP wont export longtext and it takes an act of God to get it fixed.

Answer

DeerSpotter picture DeerSpotter · Nov 24, 2015

This is what i use for all my connections for SAP:

'Connect to SAP to run automation.
        If Not IsObject(SAP_applic) Then
            Set SapGuiAuto = GetObject("SAPGUI")
            Set SAP_applic = SapGuiAuto.GetScriptingEngine
        End If
            Set connection = SAP_applic.Children(0)
        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