Skip to Content
avatar image
Former Member

Logon SAP with VBA

Hello Community,

I want to read out data from SAP through Excel with scripting. I found the following Code but it is not running. It don´t jump into the first If statement, why not?

If I comment out the first If statement I get:

Run-time error '-2147221020 ( 800401e4)' Automation error Invalid syntax

Many thanks in advance.

Public SAPguiAPP As GuiApplication  
Public oConnection As GuiConnection  
Public oSession As GuiSession

Public Sub Test()

If Not IsObject(SAPguiAPP) Then  
Set SapGuiAuto = GetObject("SAPGUI")  
Set SAPguiAPP = SapGuiAuto.GetScriptingEngine
End If

If Not IsObject(Connection) Then  
Set oConnection = SAPguiAPP.Children(0)
End If

If Not IsObject(Session) Then 
Set oSession = oConnection.Children(0)
End If

If IsObject(WScript) Then  
WScript.ConnectObject oSession, "on"  
WScript.ConnectObject SAPguiAPP, "on"
End If

oSession.FindById("wnd[0]").Maximize
oSession.FindById("wnd[0]").resizeWorkingPane 109, 26, False
oSession.FindById("wnd[0]/tbar[0]/okcd").Text = "ca03"

Set Session = Nothing  
Set Connection = Nothing  
Set SAPguiAPP = Nothing

End Sub
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Jun 09, 2017 at 11:54 AM

    Hello Lars,

    try the following code.

    Public SAPguiAPP As GuiApplication
    Public oConnection As GuiConnection
    Public oSession As GuiSession
    
    Public Sub Test()
    
      If SAPguiAPP Is Nothing Then
        Set SapGuiAuto = GetObject("SAPGUI")
        Set SAPguiAPP = SapGuiAuto.GetScriptingEngine
      End If
    
      If oConnection Is Nothing Then
        Set oConnection = SAPguiAPP.Children(0)
      End If
    
      If oSession Is Nothing Then
        Set oSession = oConnection.Children(0)
      End If
    
      oSession.FindById("wnd[0]").Maximize
      oSession.FindById("wnd[0]").resizeWorkingPane 109, 26, False
      oSession.FindById("wnd[0]/tbar[0]/okcd").Text = "ca03"
    
      Set Session = Nothing
      Set Connection = Nothing
      Set SAPguiAPP = Nothing
    
    End Sub
    

    Let us know your result.

    Cheers
    Stefan

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jun 15, 2017 at 06:29 AM

    Thanks Stefan, it is working.

    But an "o" in the following part right?

    Set oSession = Nothing
    Set oConnection = Nothing
    Set SAPguiAPP = Nothing

    Greetings

    Lars

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jan 03 at 10:14 AM

    Great, Will actions (business processes like CRM business partner creation etc.,) be automatable via this way in VBA?

    We have test data preparation automated via VBA. this one is interesting.! Share more inputs pls.

    Add comment
    10|10000 characters needed characters exceeded