如何将数据从SAP复制和粘贴到Excel?

时间:2019-05-28 11:40:24

标签: excel vba sap

我保存了脚本并将其连接到VBA,然后要将本地SAP数据复制到外部Excel文件中。

 Sub FOS()
 If Not IsObject(App) Then
 Set SapGuiAuto = GetObject("SAPGUI")
 Set App = SapGuiAuto.GetScriptingEngine
 End If
 ...
 session.findById("wnd[0]").maximize
 session.findById("wnd[0]/tbar[0]/okcd").Text = "S_ALR_87011964"
 ...
 session.findById("wnd[0]/tbar[1]/btn[19]").press
 session.findById("wnd[0]/usr/chkPA_XGBAF").Selected = True
 session.findById("wnd[0]/usr/chkPA_XGBAF").SetFocus
 session.findById("wnd[0]/tbar[1]/btn[8]").press

 End Sub()

由于VBA宏,我想自动提取数据,然后复制数据并将其传输到非常特定的excel文件中。

1 个答案:

答案 0 :(得分:0)

选择了ALV网格控件后,您可以尝试以下操作。

例如:

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

'for the German SAP version
'SAP_Workbook = "Tabelle von ALV (1)"

'for the English SAP version (?)
SAP_Workbook = "Worksheet in ALVXXL01 (1)"

EXCEL_Path = "c:\tmp\"
myWorkbook = "Report.xlsx"

Set xclwbk = Application.Workbooks.Item(SAP_Workbook)

Application.Visible = True
Application.DisplayAlerts = False

xclwbk.SaveAs Filename:=EXCEL_Path & myWorkbook, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

Set xclwbk = Nothing
'Application.Wait (Now + TimeValue("0:00:02"))
session.findById("wnd[0]/tbar[0]/btn[3]").press
Application.DisplayAlerts = True
...
End Sub

关于ScriptMan