Hi Script Man, Hello everybody,
perhaps somebody could help me with my code. I have a problem with saving the spreadsheet exported from SAP. I read all posts, but I still get an error. When I run code it generates Excel file but doesn't save it, and program is running like forever until it's stuck.
I changed German name "Tabelle von Basis (1)" to Polish name "Arkusz w basis (1)", since I have Excel in Polish.
I also deleted this line:
"Set Application = SapGuiAuto.GetScriptingEngine"
because I was getting error message for word "Application" (invalid use of property).
So, here is my code, maybe somebody could help:
...
'RECORDING ACTIONS IN SAP
If Not IsObject(Application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = Application.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject Application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/chkP_PROCES").Selected = False
session.findById("wnd[0]/usr/ctxtSEL_BUKR-LOW").Text = "038"
session.findById("wnd[0]/usr/chkP_PROCES").SetFocus
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/cntlTOP_CONTAINER/shellcont/shell").setCurrentCell -1, "WC_ICON"
session.findById("wnd[0]/usr/cntlTOP_CONTAINER/shellcont/shell").selectColumn "WC_ICON"
session.findById("wnd[0]/usr/cntlTOP_CONTAINER/shellcont/shell").pressToolbarContextButton "&MB_FILTER"
session.findById("wnd[0]/usr/cntlTOP_CONTAINER/shellcont/shell").selectContextMenuItem "&FILTER"
session.findById("wnd[1]").sendVKey 4
session.findById("wnd[2]").sendVKey 2
session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/btn%_%%DYN001_%_APP_%-VALU_PUSH").press
session.findById("wnd[2]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,1]").SetFocus
session.findById("wnd[2]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,1]").caretPosition = 0
session.findById("wnd[2]").sendVKey 4
session.findById("wnd[3]/usr/lbl[1,5]").SetFocus
session.findById("wnd[3]/usr/lbl[1,5]").caretPosition = 0
session.findById("wnd[3]").sendVKey 2
session.findById("wnd[2]/tbar[0]/btn[8]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/cntlTOP_CONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
session.findById("wnd[0]/usr/cntlTOP_CONTAINER/shellcont/shell").selectContextMenuItem "&XXL"
session.findById("wnd[1]/usr/cmbG_LISTBOX").Key = "08"
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
'code from forum:
SAP_Workbook = "Arkusz w Basis (1)"
EXCEL_Path = "C:\Users\msuzdorf\Desktop"
myWorkbook = "Report.xlsx"
On Error Resume Next
Do
Err.Clear
Set xclapp = GetObject(, "Excel.Application")
If Err.Number = 0 Then Exit Do
'MsgBox "Wait for Excel session"
WScript.sleep 2000
Loop
Do
Err.Clear
Set xclwbk = xclapp.Workbooks.Item(SAP_Workbook)
If Err.Number = 0 Then Exit Do
'MsgBox "Wait for SAP workbook"
WScript.sleep 2000
Loop
On Error GoTo 0
Set xclsheet = xclwbk.Worksheets(1)
xclapp.Visible = True
xclapp.DisplayAlerts = False
xclapp.ActiveWorkbook.SaveAs EXCEL_Path & myWorkbook
xclapp.ActiveWorkbook.Close
Set xclwbk = Nothing
Set xclsheet = Nothing
xclapp.Quit
Set xclapp = Nothing
Thank you in advance.