cancel
Showing results for 
Search instead for 
Did you mean: 

Variable declaration in sap logon

Former Member
0 Kudos

Dear Experts,

Please note that I’m new in VBA and SAP GUI SCRITPING. I just read few discussion about with SAP LOGON, most of users use early binding to establish connection with SAP.

As far I'm concerned I use late binding. Please find below:

Set Connection = GetObject("SAPGUI").GetScriptingEngine.Children(0)

If Not IsObject(SAPGUIApp) Then

   Set SapGuiAuto = GetObject("SAPGUI")

   Set SAPGUIApp = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(Connection) Then

   Set Connection = SAPGUIApp.Children(0)

End If

If Not IsObject(SAP_Session) Then

   Set session = Connection.Children(0)

End If

If IsObject(WScript) Then

   WScript.ConnectObject session, "on"

   WScript.ConnectObject SAPGUIApp, "on"

End If

I would like to use OPTION EXPLICIT in my codes, but that requires also Variable declaration for that part. I already add SAP GUI SCRIPTING API library in references, bo still i don't know objects there.

Can anyone help me out with that variable declaration?

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor
0 Kudos

Hello Tomasz,

welcome in the SAP GUI Scripting forum.

You add at first the SAP GUI Scripting API library as reference to your VBA project from the Tools menu.

Now you can use this code with explicit variable declaration:

Option Explicit

Sub Test()

  On Error Resume Next

  Dim SapGuiAuto As Object

  Dim SAPGUIApp As SAPFEWSELib.GuiApplication

  Dim Connection As SAPFEWSELib.GuiConnection

  Dim Session As SAPFEWSELib.GuiSession

  Set SapGuiAuto = GetObject("SAPGUI")

  If SapGuiAuto Is Nothing Then

    Exit Sub

  End If

  Set SAPGUIApp = SapGuiAuto.GetScriptingEngine

  If SAPGUIApp Is Nothing Then

    Exit Sub

  End If

  Set Connection = SAPGUIApp.Children(0)

  If Connection Is Nothing Then

    Exit Sub

  End If

  Set Session = Connection.Children(0)

  If Session Is Nothing Then

    Exit Sub

  End If

  'Your Code here

End Sub

Good luck.

Cheers

Stefan

Former Member
0 Kudos

Thx, works like a charm!

Answers (0)