Skip to Content
avatar image
Former Member

Opening SAP using macro

Hi everyone,

with the code i was able to open a connection to SAP .

Set sap = CreateObject("SapAutoGui.Event")

host = "test12"
sapNr = "90"
mandant = "800"
user =" test1"
Password = "pass"

bOK = sap.Connect(host , sapNr ,SapGuiActiveX Or SapGuiFront Or SapGuiFullMenu)
OKCheck bOK, "Error in opening connection"

iCtrl = sap.FindByField("RSYST-MANDT", 0, SapEdit)
bOK = sap.SetControlValue(iCtrl, mandant)
OKCheck bOK, "Error in setting text value"

iCtrl = sap.FindByField("RSYST-BNAME", 0, SapEdit)
bOK = sap.SetControlValue(iCtrl, user )
OKCheck bOK, "Error in setting text value"

iCtrl = sap.FindByField("RSYST-BCODE", 0, SapPassword)

bOK = sap.SetControlValue(iCtrl, Password)
OKCheck bOK, "Error in setting text value"

bOK = sap.SetCursorByControl(iCtrl)
OKCheck bOK, "Error in setting cursor position"

bOK = sap.SendEvent
OKCheck bOK, "Error in sending default key"

but since SNC is active, i can't open sap

how do I make a connection to SAP when SNC = true

Can anyone help me?



Add comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

1 Answer

  • Best Answer
    May 18, 2013 at 07:46 AM

    Hello Saad,

    '--------------------------Start Excel VBA Sub-----------------------------

    Sub SAP_OpenSessionFromLogon()
    Dim SapGui
    Dim Applic
    Dim connection
    Dim session

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

    Set SapGui = GetObject("SAPGUI")

    Rem Create the GuiApplication object
    Set Applic = SapGui.GetScriptingEngine

    Rem Open a connection
    Set connection = Applic.OpenConnection("QE1 - ERP QA ECC 6.0", True) '<=== here you need to fillin your connection description

    Set session = connection.Children(0)

    session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "800"
    session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "Test1"
    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

    '--------------------------End of Excel VBA Sub----------------------------

    Just fillin the description from your system in row where 'Set connection' is defined.

    If you have any questions let me know. Br, Holger

    Add comment
    10|10000 characters needed characters exceeded