Skip to Content
author's profile photo Former Member
Former Member

How I open the SAP Logon by VBA Macro

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

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Posted on Dec 17, 2015 at 07:58 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.