cancel
Showing results for 
Search instead for 
Did you mean: 

How to paste data from SAP to Excel

0 Kudos

Hi everyone,

I'm a beginner in terms of SAP macro, and I'm stuck with one, I would like to copy a selection from SAP to Excel, would you be able to help me finding the missing part of the code?

Sub MasterC()

Set SapGuiAuto = GetObject("SAPGUI")

Set SapApp = SapGuiAuto.GetScriptingEngine

Set SapConnection = SapApp.Children(0)

Set SAPsession = SapConnection.Children(0)

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

SAPsession.findById("wnd[0]/tbar[0]/okcd").Text = "/nvdh2n"

SAPsession.findById("wnd[0]").sendVKey 0

SAPsession.findById("wnd[0]/usr/ctxtS_HITYP").Text = "a"

SAPsession.findById("wnd[0]/usr/ctxtS_DATE").Text = "22.09.2015"

SAPsession.findById("wnd[0]/usr/ctxtS_KUNNR-LOW").Text = "10032632"

SAPsession.findById("wnd[0]/usr/ctxtS_KUNNR-LOW").SetFocus

SAPsession.findById("wnd[0]/usr/ctxtS_KUNNR-LOW").caretPosition = 8

SAPsession.findById("wnd[0]").sendVKey 8

SAPsession.findById("wnd[0]/shellcont/shell").selectItem "          1", "Column1"

'I would like to paste the item above (simple name) into an Excel cell

End Sub

Thanks a lot guys

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor
0 Kudos

Hello Florian,

welcome to Scripting Language forum.

You can try this to read the first column of the first entry of a TableTreeControl:

Sub MasterC()

 

  Set SapGuiAuto = GetObject("SAPGUI")

  Set SapApp = SapGuiAuto.GetScriptingEngine

  Set SapConnection = SapApp.Children(0)

  Set SAPSession = SapConnection.Children(0)

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

  SAPSession.findById("wnd[0]/tbar[0]/okcd").Text = "/nvdh2n"

  SAPSession.findById("wnd[0]").sendVKey 0

  SAPSession.findById("wnd[0]/usr/ctxtS_HITYP").Text = "a"

  SAPSession.findById("wnd[0]/usr/ctxtS_DATE").Text = "22.09.2015"

  SAPSession.findById("wnd[0]/usr/ctxtS_KUNNR-LOW").Text = "10032632"

  SAPSession.findById("wnd[0]/usr/ctxtS_KUNNR-LOW").SetFocus

  SAPSession.findById("wnd[0]/usr/ctxtS_KUNNR-LOW").caretPosition = 8

  SAPSession.findById("wnd[0]").sendVKey 8

  Set Tree = SAPSession.findById("wnd[0]/shellcont/shell")

  topNode = CStr(Tree.topNode)

  Key = topNode

  nodeName = Tree.GetNodeTextByKey(Key)

  colName = Tree.GetColumnNames.Item("0")

  Set col = Tree.GetColumnCol(colName)

  Sheet1.Cells(1, 1) = col.Item(0)

End Sub

You can find an excellent discussion and solution how to read columns of a TableTreeControl here.

Let us know your results.

Cheers

Stefan

0 Kudos

Thanks a lot Stefan it's working

Answers (0)