Excel VBA 无法完成从 SAP 导出 xlsx

发布于 2025-01-20 02:35:18 字数 2258 浏览 4 评论 0原文

我是 Sap 脚本的绝对初学者,而且只是 Excel Vba 的初学者(我的稀缺知识主要来自你们的社区),所以请耐心等待。

我正在尝试从 Excel Vba 启动 Sap 中的数据提取并将导出保存在 xlsx 文件中。 虽然我可以在 txt 中完成文件保存,但在 xlsx 中却无法完成此操作。它停止在 Sap 窗口“保存”等待,尽管已经给出了文件名、路径并返回: here where it stop

我在这里给你代码,以及导出 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:
here where it stops

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文