0
votes

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
1
Comments are not for extended discussion; this conversation has been moved to chat.Bhargav Rao
I can recommend this link to you: stackoverflow.com/questions/19452461/…ScriptMan
@ScriptMan Thanks! That initial set up for the SAP Gui auto helped. Your a life saver.Daume

1 Answers

0
votes

You say that appl contains Nothing. So it's normal that session also contains Nothing, hence the error 91 when the variable is used.

As @GSerg said in the conversation:

That would be because IsObject determines if the variable has an object type, not whether it contains an instance of an object. In particular, IsObject(Nothing) is True. You want to replace all your IsObject calls with Is Nothing tests.

Consequently, the first lines should be:

        If appl Is Nothing Then
           Set SapGuiAuto = GetObject("SAPGUI")
           Set appl = SapGuiAuto.GetScriptingEngine
        End If
        If Connection Is Nothing Then
           Set Connection = appl.Children(0)
        End If
        If session Is Nothing Then
           Set session = Connection.Children(0)
        End If