cancel
Showing results for 
Search instead for 
Did you mean: 

Can't figure out what is the code

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

michael_hobbins
Active Participant
0 Kudos

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

Former Member
0 Kudos

Hi thanks for response

It seems all right. I guess something else is a problem.

It points out Set Application = SapGuiAuto.GetScriptingEngine  

"Application" Word in this line and error message says invalid use of property

Any idea I got SAP and proper transaction open and Excel ?

Thanks

michael_hobbins
Active Participant
0 Kudos

Excellent. Have't worked with this scripting code before. Check http://scn.sap.com/thread/49702 to see if it wil solve the problem.

Michael

holger_khn
Contributor
0 Kudos

Hello.

Application is reserved in Excel VBA. Use instead of variable Application for example SAPAPPL

Then it should work.

Cheers,

Holger

Former Member
0 Kudos

I got finally this code and it gives an error 424 Object Required

in this line : session.findById("wnd[0]").maximize

Sub sss()

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

'RemSet up connection to the SAP screen:

If Not IsObject(SAPGuiApp) Then

   Set SAPGuiAuto = GetObject("SAPGUI")

   Set SAPGuiApp = SAPGuiAuto.GetScriptingEngine

End If

If Not IsObject(Connection) Then

   Set Connection = SAPGuiApp.Children(0)

End If

If Not IsObject(SAP_Session) Then

   Set SAP_Session = Connection.Children(0)

End If

If IsObject(WScript) Then

   WScript.ConnectObject SAP_Session, "on"

   WScript.ConnectObject SAPGuiApp, "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

End Sub

script_man
Active Contributor
0 Kudos

Hi Dominik,

You are approaching the goal. My suggestion is as follows:

1. either

Set SAP_Session = Connection.Children(0)

and all others commands with session as e.g.

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

. . .

2. or

Set session = Connection.Children(0)

and all others commands with session as e.g.

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

  . . .

Regards,

ScriptMan

Former Member
0 Kudos

Ok macro works and triggers actions from recorder. I was doing it with hope I will select data in SAP and paste in Excel AND/OR export file from SAP with data.

If I record select column and then copy it. Script is recorded until selection . It ignores copy (i tried use mouse right click and copy AND manual alt+F12 and then copy from context menu)

When I export recorder does open export window but does not writes the line relevant to clicking save.

I found such bits

  '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



But they dont seem to be relevant to my previous macro.


Any quick fix to get data out of Excel


script_man
Active Contributor
0 Kudos

Congratulations!

In order to solve the next problem, you must do it yourself.

Check the following link:     http://scn.sap.com/thread/3450067

Answers (0)