cancel
Showing results for 
Search instead for 
Did you mean: 

How I open the SAP Logon by VBA Macro

Former Member
0 Kudos

Hi, guys.

I am trying to open the SAP Logon By macro, but the program was until the red line below. The VBA showed the error message "The enumerator of the collection cannot find en element with thye specified index"

Do you know what's happening?

Sub SAP_OpenSessionFromLogon()

Dim SapGui

Dim Applic

Dim connection

Dim session

Shell ("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapfewcp.exe")

Set SapGui = GetObject("SAPGUI")

Rem Create the GuiApplication object

Set Applic = SapGui.GetScriptingEngine

Rem Open a connection

Set connection = Applic.OpenConnection("QLA - ECC Project One Quality System", True) '<=== here you need to fillin your connection description

Set session = connection.Children(0)

session.findById("wnd[0]").maximize

session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "120"

session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "MASOUZA"

session.findById("wnd[0]").sendVKey 0

Rem Do something: Either fill out the login screen

Rem or in case of Single-Sign-On start a transaction.

session.SendCommand ("/nbibs")

MsgBox "Waiting..."

Rem Shutdown the connection

Set session = Nothing

connection.CloseSession ("ses[0]")

Set connection = Nothing

Set sap = Nothing

MsgBox "Done"

End Sub

Accepted Solutions (0)

Answers (1)

Answers (1)

stefan_schnell
Active Contributor
0 Kudos

Hello Fulvio,

your VBA code looks very good. All I changed I marked red. I start saplogon.exe and not sapfewcp.exe, because in the saplogon.exe is the SAPGUI object. And I wait with a loop until the saplogon window is available and therewith the object SAPGUI.

Sub SAP_OpenSessionFromLogon()

  Dim SapGui

  Dim Applic

  Dim connection

  Dim session

  Dim WSHShell

  Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus

  Set WSHShell = CreateObject("WScript.Shell")

  Do Until WSHShell.AppActivate("SAP Logon ")

    Application.Wait Now + TimeValue("0:00:01")

  Loop

  Set WSHShell = Nothing

  Set SapGui = GetObject("SAPGUI")

  Set Applic = SapGui.GetScriptingEngine

  Set connection = Applic.OpenConnection("QLA - ECC Project One Quality System", True)

  Set session = connection.Children(0)

  session.findById("wnd[0]").maximize


  session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "120"

  session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "MASOUZA"

  session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "secret"

  session.findById("wnd[0]").sendVKey 0

  session.SendCommand ("/nBIBS")

  MsgBox "Waiting..."

  Set session = Nothing

  connection.CloseSession ("ses[0]")

  Set connection = Nothing

  Set sap = Nothing


  MsgBox "Done"

End Sub

Let us know your results.

Cheers

Stefan