Skip to Content
avatar image
Former Member

Can't figure out what is the code

Hi


I got following macro that will help me to run certain transaction.

I just can't figure out where comments ends and where code starts so I was unable to run it for like 3 hours now.


In other words


I am trying to use Excel Macro that will get to active transaction in SAP and do waht I have recorded.



'SAPGUI Macro for updating SAP from Excel data 'Prerequisites: '1. Script Recording and Playback from the SAPGui must be enabled by admin '2. The Excel list must be set up from top left corner of a worksheet '3. The worksheet with data must be the active sheet '4. There is no error handling. Build it yourself! '5. The script will update the active (current) SAP transaction'Instructions for use 'Record a macro of your update actions. 'Paste the session.findById stuff between the <== and ==> markers'Edit the literal values and replace with Trim(CStr(objSheet.Cells(x, y).Value)) 'where x and y are the row and column counters'Open up your Excel sheet with data (row 1 is ignored here) 'Fire the macro! '===========BEGIN BOILERPLATE=================== REMSet up connection to the SAP screen: 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 Setsession = connection.Children(0) End If If IsObject(WScript) Then WScript.ConnectObject session, "on" WScript.ConnectObject application, "on" End If REMSet up connection to the Excel data to be keyed into SAP: Dim objExcel Dim objSheet, intRow, i Set objExcel = GetObject(,"Excel.Application") Set objSheet = objExcel.ActiveWorkbook.ActiveSheet session.findById("wnd[0]").maximize

'===========END BOILERPLATE=====================


'REM Example: Mark material for deletion in Plant using MM06 For i = 2 to objSheet.UsedRange.Rows.Count     'Assuming there is a header rowREM Find a cell and put its value in: REM Note: The section of the Script shown below can be recorded ' and edited to pick data from the Excel sheetstrCN = Trim(CStr(objSheet.Cells(i, 1).Value)) strWerks = Trim(CStr(objSheet.Cells(i, 2).Value)) '<== session.findById("wnd[0]/usr/ctxtRM03G-MATNR").text = strCN session.findById("wnd[0]/usr/ctxtRM03G-WERKS").text = strWerks session.findById("wnd[0]/tbar[0]/btn[0]").press session.findById("wnd[0]/usr/chkRM03G-LVOMA").selected = false session.findById("wnd[0]/usr/chkRM03G-LVOWK").selected = truesession.findById("wnd[0]/tbar[0]/btn[11]").press 'session.findById("wnd[0]/tbar[0]/btn[0]").press '==> Next
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    avatar image
    Former Member
    Jun 24, 2014 at 01:23 PM

    Hi Dominik, see if the following works

    'SAPGUI Macro for updating SAP from Excel data

    'Prerequisites:

    '1. Script Recording and Playback from the SAPGui must be enabled by admin

    '2. The Excel list must be set up from top left corner of a worksheet

    '3. The worksheet with data must be the active sheet

    '4. There is no error handling. Build it yourself!

    '5. The script will update the active (current) SAP transaction

    'Instructions for use

    'Record a macro of your update actions.

    'Paste the session.findById stuff between the <== and ==> markers

    'Edit the literal values and replace with Trim(CStr(objSheet.Cells(x, y).Value))

    'where x and y are the row and column counters

    'Open up your Excel sheet with data (row 1 is ignored here)

    'Fire the macro!

    '===========BEGIN BOILERPLATE===================

    'RemSet up connection to the SAP screen:

    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

        Setsession = Connection.Children(0)

    End If

    If IsObject(WScript) Then

        WScript.ConnectObject session, "on"

        WScript.ConnectObject Application, "on"

    End If

    'REMSet up connection to the Excel data to be keyed into SAP:

    Dim objExcel

    Dim objSheet, intRow, i

    Set objExcel = GetObject(, "Excel.Application")

    Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

    session.findById("wnd[0]").maximize

    '===========END BOILERPLATE=====================

    'REM Example: Mark material for deletion in Plant using MM06

    For i = 2 To objSheet.UsedRange.Rows.Count

        'Assuming there is a header row

        'REM Find a cell and put its value in:

        'REM Note: The section of the Script shown below can be recorded

        ' and edited to pick data from the Excel

        sheetstrCN = Trim(CStr(objSheet.Cells(i, 1).Value))

        strWerks = Trim(CStr(objSheet.Cells(i, 2).Value))

        '<==

        session.findById("wnd[0]/usr/ctxtRM03G-MATNR").Text = strCN

        session.findById("wnd[0]/usr/ctxtRM03G-WERKS").Text = strWerks

        session.findById("wnd[0]/tbar[0]/btn[0]").press

        session.findById("wnd[0]/usr/chkRM03G-LVOMA").Selected = False

        session.findById("wnd[0]/usr/chkRM03G-LVOWK").Selected = True

        session.findById("wnd[0]/tbar[0]/btn[11]").press

        session.findById("wnd[0]/tbar[0]/btn[0]").press

        '==>

    Next

    Cheers,

    Michael

    Add comment
    10|10000 characters needed characters exceeded