cancel
Showing results for 
Search instead for 
Did you mean: 

Logon SAP with VBA

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor
0 Kudos

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 oSession = Nothing
  Set oConnection = Nothing
  Set SAPguiAPP = Nothing

End Sub

Let us know your result.

Cheers
Stefan

nachodrums
Explorer
0 Kudos

It is not working for me. I have the error at the public declarations. Do I have something wrong?

Answers (2)

Answers (2)

Former Member

Thanks Stefan, it is working.

But an "o" in the following part right?

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

Greetings

Lars

stefan_schnell
Active Contributor
0 Kudos

Hello Lars,

you are right.

Cheers
Stefan

Former Member
0 Kudos

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.