Skip to Content
avatar image
Former Member

VBA automation to read from SAP


Does anyone know how to read from an SAP screen using VBA code? I am NOT referring to reading from a table like VBAK, MARA etc.

The requirement is to enter a transaction code lets say VA03/VA43, enter a document/order number and read the contents of the screen using the screen field name like VBELN, KUNNR etc.


Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • avatar image
    Former Member
    Apr 26, 2010 at 04:19 PM

    In SAP transactions, the data elements appearing on the screen are in structures or table controls usually. So, while we don't do screen-scraping, code that obtains the data values from the screen structures is possible, often in exits.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 27, 2010 at 01:17 PM

    Using vbs:

    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]/tbar[0]/okcd").text = "VA03"


    session.findById("wnd[0]/usr/ctxtSOMEFIELDNAME").text = some_variable_you_can_store_later

    etc, etc

    I am not familiar with tath screen so you might need more code in wnd[0]/usr/ctxtSOMEFIELDNAME if there are multiple tabs.

    It will be a lot easier to make SAP record the script: ALT+F12 -> "Script Recording and Playback", etc then go to the file which is saved in your local SAP directory and see what SAP did.

    Hope this helps

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member


      I just realised I made a mistake on a line above, I meant to say:

      some_variable_you_can_store_later = session.findById("wnd[0]/usr/ctxtSOMEFIELDNAME").text

      (I had flipped the order)

      When I first started writing these I had scripting disabled. So I went to ALT+F12 -> Scripting and then enabled it.

      Unless even that option is not there for some higher level security setting.

      Good luck.