Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Variable declaration in sap logon

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?

Former Member
replied

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

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question