Excel VBA 无法完成从 SAP 导出 xlsx
我是 Sap 脚本的绝对初学者,而且只是 Excel Vba 的初学者(我的稀缺知识主要来自你们的社区),所以请耐心等待。
我正在尝试从 Excel Vba 启动 Sap 中的数据提取并将导出保存在 xlsx 文件中。 虽然我可以在 txt 中完成文件保存,但在 xlsx 中却无法完成此操作。它停止在 Sap 窗口“保存”等待,尽管已经给出了文件名、路径并返回:
我在这里给你代码,以及导出 xlsx 例程(我需要你的帮助)和 txt输出(有效):
enter code here
Public Sub SAP_Export()
Set SapGuiAuto = GetObject("SAPGUI") 'Get the SAP GUI Scripting object
Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
'This My SAP Script - it does't matter, can be any Sap script:
'/////
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "zbrgcogeban" 'transaction name
session.findById("wnd[0]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/txtS_CONTO-LOW").Text = "69314*" 'variable
session.findById("wnd[0]/usr/txtS_CONTO-LOW").SetFocus
session.findById("wnd[0]/usr/txtS_CONTO-LOW").CaretPosition = 6
session.findById("wnd[0]/tbar[1]/btn[8]").press
'/////
GoTo SAVEASXLSX
'****THIS SAVE AS TXT...THIS WORKS!..********
'Click the export to file button
session.findById("wnd[0]/tbar[1]/btn[45]").press 'SAVE LIST IN FILE...txt
'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 and path
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "test.txt"
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\appo\"
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Export the file
SAVEASXLSX:
'THIS SHOULD SAVE AS XLSX BUT IT DOESN'T WORK, IT REMAIN ON THE "SAVE AS" WINDOW WAITING SOMEONE PRESSING "SAVE" BUTTON:
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select 'open "save as xlsx" as selecting in Sap menu: List->Export->Spreadsheet
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "test.xlsx" 'file name
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\appo\" 'path name
session.findById("wnd[1]/tbar[0]/btn[0]").press
End Sub
I'm an absolute beginner in Sap script and just a beginner in Excel Vba (my scarse knowledge comes mostly from your kind community), so be patient.
I'm trying to launch from Excel Vba a data estraction in Sap and save the export in a xlsx file.
While I can complete the file save in txt I cannot do so in xlsx. It stops in the Sap window "save" waiting, despite having given a file name, path and return:
I give you here the code, with export xlsx routine (where I need your help) and with the txt output (that woorks):
enter code here
Public Sub SAP_Export()
Set SapGuiAuto = GetObject("SAPGUI") 'Get the SAP GUI Scripting object
Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
'This My SAP Script - it does't matter, can be any Sap script:
'/////
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "zbrgcogeban" 'transaction name
session.findById("wnd[0]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/txtS_CONTO-LOW").Text = "69314*" 'variable
session.findById("wnd[0]/usr/txtS_CONTO-LOW").SetFocus
session.findById("wnd[0]/usr/txtS_CONTO-LOW").CaretPosition = 6
session.findById("wnd[0]/tbar[1]/btn[8]").press
'/////
GoTo SAVEASXLSX
'****THIS SAVE AS TXT...THIS WORKS!..********
'Click the export to file button
session.findById("wnd[0]/tbar[1]/btn[45]").press 'SAVE LIST IN FILE...txt
'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 and path
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "test.txt"
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\appo\"
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Export the file
SAVEASXLSX:
'THIS SHOULD SAVE AS XLSX BUT IT DOESN'T WORK, IT REMAIN ON THE "SAVE AS" WINDOW WAITING SOMEONE PRESSING "SAVE" BUTTON:
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select 'open "save as xlsx" as selecting in Sap menu: List->Export->Spreadsheet
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "test.xlsx" 'file name
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\appo\" 'path name
session.findById("wnd[1]/tbar[0]/btn[0]").press
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论