I have recorded a sequence in SAP, and I have passed the code to the Excel developer. In that sequence, the only thing I intend to do is to open a query about it when opening the Excel workbook. I do not have the loop implemented to get all the rows of the query yet, because I want it to work at least first. It is the first time that I try to automate the export of data from SAP to Excel, the idea is to avoid repeating the process of downloading the query from SAP, and to do it directly when manipulating this Excel. I also know that at the moment I keep it in a txt. (Passing it later to Excel would be minor problems). I do not intend to deal with the error, I want to obtain the data of this transaction daily. I leave the code here:
Private Sub Workbook_Open()
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
'Start the transaction to view a table
session.StartTransaction "PSG"
'Select table
MsgBox "Todo bien"
session.findById("wnd[0]/usr/ctxtS1_LGNUM").SetFocus
session.findById("wnd[0]/usr/ctxtS1_LGNUM").Text = "201"
session.findById("wnd[0]/tbar[1]/btn[7]").press
'Set our selection criteria
session.findById("wnd[0]/usr/txtMAX_SEL").Text = "2"
session.findById("wnd[0]/tbar[1]/btn[8]").press
'Click the export to file button
session.findById("wnd[0]/tbar[1]/btn[9]").press
'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
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\user001\Desktop"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "prueba.txt"
'Export the file
session.findById("wnd[1]/tbar[0]/btn[0]").press
End Sub
Error 619 comes out when we do the first findById (the MsgBox runs correctly). Skip when we select or change the field to choose the warehouse from which we want to extract the data.