I'm trying to connect to SAP through Excel VBA to run a recorded macro.
When it reaches the start of actual SAP code it places the
Run-Time Error '91'
on the line session.FindById("wnd[0]").Maximize
.
If I delete that line it has the same issues with every session line.
I verified the references and declarations.
I ran it as a VBS script and it worked.
I'm logged into a SAP session before running the code.
appl
contains Nothing in the value of the Local Windows. On the Type it contains GuiApplication.
Here is the snip of the code:
Private Sub CommandButton1_Click()
Dim Tablename As String
If TextBox1 = "" Then
If Not IsObject(appl) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set appl = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = appl.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]/tbar[0]/okcd").Text = "Stock01"
session.FindById("wnd[0]/tbar[0]/btn[0]").press
session.FindById("wnd[0]/usr/ctxtS_MATNR-LOW").Text = "566666"
session.FindById("wnd[0]/usr/ctxtS_MATNR-HIGH").Text = "5666666"
session.FindById("wnd[0]/usr/ctxtS_WERKS-LOW").Text = "1111"
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").Text = "1045"
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").SetFocus
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").caretPosition = 4
session.FindById("wnd[0]/tbar[1]/btn[8]").press
session.FindById("wnd[0]/usr/cntlYCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
session.FindById("wnd[0]/usr/cntlYCONTAINER/shellcont/shell").selectContextMenuItem "&XXL"
session.FindById("wnd[1]/tbar[0]/btn[0]").press
session.FindById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Documents\SAP_GUI_Code"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Stock.XLSX"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 11
session.FindById("wnd[1]/tbar[0]/btn[11]").press
session.FindById("wnd[0]/tbar[0]/btn[3]").press
session.FindById("wnd[0]/tbar[0]/btn[3]").press
exit Sub