Skip to Content
0

Logon SAP with VBA

Jun 07, 2017 at 08:35 PM

177

avatar image
Former Member

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
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Stefan Schnell
Jun 09, 2017 at 11:54 AM
0

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

Share
10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Jun 15, 2017 at 06:29 AM
0

Thanks Stefan, it is working.

But an "o" in the following part right?

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

Greetings

Lars

Show 1 Share
10 |10000 characters needed characters left characters exceeded

Hello Lars,

you are right.

Cheers
Stefan

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

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.

Share
10 |10000 characters needed characters left characters exceeded