1 year ago
#386086
TheGrill Firenze
Excel VBA cannot complete an export in xlsx from SAP
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
excel
vba
save
xlsx
sap-gui
0 Answers
Your Answer