提问人:Sidvi 提问时间:11/8/2017 最后编辑:Sandra RossiSidvi 更新时间:6/21/2023 访问量:18211
将特定单元格数据从 SAP GUI 获取到 Excel
Getting specific cell data from SAP GUI to Excel
问:
我正在尝试使用VBA将数据从SAP GUI中一行中的特定单元格中获取到Excel表格中。当我录制我单击所需的 5-6 个项目的宏时,它只给了我单击一个。此外,我看不到任何将数据从单元格获取到SAP GUI的方法。SAP GUI 代码如下:
Dim W_Ret As Boolean
W_Ret = Attach_Session
If Not W_Ret Then
Exit Sub
End If
startrow = 10
On Error GoTo myerr
objSess.findById("wnd[0]").maximize
objSess.findById("wnd[0]/tbar[0]/okcd").Text = "/nymm_pricelist"
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/ctxtS_MATNR-LOW").Text = Cells(currentline, 1).Value
objSess.findById("wnd[0]/usr/ctxtS_VKORG-LOW").Text = Cells(currentline, 4).Value
objSess.findById("wnd[0]/usr/ctxtS_VKORG-LOW").SetFocus
objSess.findById("wnd[0]/usr/ctxtS_VKORG-LOW").caretPosition = 4
objSess.findById("wnd[0]").sendVKey 8
Set myGrid = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell")
Cells(currentline, 5).Value = myGrid.GetCellValue(0, "MAKTX")
Cells(currentline, 6).Value = myGrid.GetCellValue(0, "GLOBALSALES_KBETR")
Cells(currentline, 7).Value = myGrid.GetCellValue(0, "GLOBALSALES_KONWA")
Cells(currentline, 8).Value = myGrid.GetCellValue(0, "GLOBALTRANSFER_KBETR")
Cells(currentline, 9).Value = myGrid.GetCellValue(0, "GLOBALTRANSFER_KONWA")
' Setting the line status to completed
Cells(currentline, 2).Value = 1
Exit Sub
myerr:
' Some error occured
' Setting the line status to Failed
Cells(currentline, 2).Value = 2
任何帮助都是值得赞赏的!
答:
我解决这些问题如下。
首先,我录制一个帮助脚本。这样一来,网格的相关列一个接一个地标记,例如按升序排序。以这种方式处理所有感兴趣的列后,您可以在录制的脚本中发现所有列名。
帮助脚本应如下所示:
. . . session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").currentCellColumn = "GLOBALTRANSFER_KONWA" session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectColumn "GLOBALTRANSFER_KONWA" session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").pressToolbarButton "&SORT_ASC"
在正确的脚本中,例如:
. . . session.findById("wnd[0]").maximize session.findById("wnd[0]/tbar[0]/okcd").text = "/nymm_pricelist" session.findById("wnd[0]").sendVKey 0 session.findById("wnd[0]/usr/ctxtS_MATNR-LOW").text = Cells(currentline, 1).Value session.findById("wnd[0]/usr/ctxtS_VKORG-LOW").text = Cells(currentline, 4).Value session.findById("wnd[0]/usr/ctxtS_VKORG-LOW").setFocus session.findById("wnd[0]/usr/ctxtS_VKORG-LOW").caretPosition = 4 session.findById("wnd[0]").sendVKey 8 set myGrid = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell") 'myPar_1 = myGrid.getcellvalue (0 , "X") 'myPar_2 = myGrid.getcellvalue (0 , "Y") 'myPar_3 = myGrid.getcellvalue (0 , "Z") 'myPar_4 = myGrid.getcellvalue (0 , "U") 'myPar_5 = myGrid.getcellvalue (0 , "V") myPar_6 = myGrid.getcellvalue (0 , "GLOBALTRANSFER_KONWA")
对于字符串 X - V,应使用有效的列名。
问候 脚本人
评论
脚本人的回答效果很好。对于那些一开始没有掌握它的人:
您可以通过在 SAP 中指定文本位置来创建存储文本的变量。例:
value = session.FindById("wnd[0]/usr/tblSAPL/ctxtAFVGD[4,0]").Text
这会将 SAP 文本保存到 value 变量中。
用通俗易懂的英语:
value = session.FindById("wnd[0]/usr/TableElement/CellElement[x,y]").Text
' where
' wnd[0]/usr is your SAP window, starting with 0 for the first open window
' TableElement and CellElement are the table and cell name, respectively
' x is column number
' y is row number
' By recording a SAP script and clicking around on the table you can figure out how the elements are named
评论