cancel
Showing results for 
Search instead for 
Did you mean: 

How to connect SAP using VBA code?

Former Member
0 Kudos

Hi Experts,

Could you please anyone provide the VBA code to connect SAP through EXCEL.

Thanks in advance.

Arun

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

  On Error Resume Next

If Not IsObject(SAP_applic) Then

        Set SapGuiAuto = GetObject("SAPGUI")

        Set SAP_applic = SapGuiAuto.GetScriptingEngine

End If

er = Err.Number

On Error GoTo 0

If er <> 0 Then

    er = 0

    On Error Resume Next

 

    Set sapSHShell = CreateObject("WScript.Shell")

quotes = Chr(34)

sapSHShell.Run quotes & "C:\Program Files\SAP\FrontEnd\SAPgui\saplgpad.exe" & quotes, 2, False

    On Error GoTo 0

  

    waitTill = Now() + TimeValue("00:00:01")

    While Now() < waitTill

     DoEvents

    Wend

    er = 0

    On Error Resume Next

    If Not IsObject(SAP_applic) Then

        Set SapGuiAuto = GetObject("SAPGUI") 'Setting

        Set SAP_applic = SapGuiAuto.GetScriptingEngine

    End If

 

    er = Err.Number

    On Error GoTo 0

    

    If er <> 0 Then

        tmp = MsgBox("SAP Logon not installed on your system" & vbCrLf & _

            "Please install it using eSupport!", vbInformation)

        End

    End If

End If

Set Connection = SAP_applic.openconnection("GIJ")

Set Session = Connection.Children(0)

Session.findById("wnd[0]").resizeWorkingPane 148, 21, False

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

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

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

Session.findById("wnd[0]/usr/pwdRSYST-BCODE").SetFocus

Session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8

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

Answers (0)