I have a macro code which downloads an excel file from SAP, post which it should do some data manipulation on it to arrive at my final output file. The code downloads and has the excel file opened. But still it shows an error every time that object is out of range. Once I click somewhere in the excel sheet and continue to run my code, it works perfectly after that. How to avoid this manual intervention. Kindly advise.
If Not IsObject(App) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = App.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 App, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "stark"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "*****"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").SetFocus
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 12
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/okcd").Text = "ABC01"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/radMADE").SetFocus
session.findById("wnd[0]/usr/radMADE").Select
session.findById("wnd[0]/mbar/menu[0]/menu[0]").Select
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Users\Stark\Desktop\SAP Scripting"
session.findById("wnd[1]/usr/ctxtDY_PATH").SetFocus
session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 44
session.findById("wnd[1]/tbar[0]/btn[0]").press
Windows("EXPORT.xlsx").Activate
Range("AA1").Select
ActiveCell.FormulaR1C1 = "1"
Range("AA1").Select
Selection.Copy
Range("B2:D2").Select
Range(Selection, Selection.End(xlDown)).Select
The file always gets downloaded as EXPORT.xlsx. I get an error at Windows("EXPORT.xlsx").Activate
Once I enter the sheet manually and continue running the code, it then works.
Kindly advise.