cancel
Showing results for 
Search instead for 
Did you mean: 

Need help to convert script from VBS to VBA (Excel Macro).

Former Member
0 Kudos

Please help me to convert the following script from VBS to VBA (Excel Macro). Instead of open SAP log on and type User and Password every day. I want it to open it from an excel file.

Here is the current script I have, but for some reason the SAP popup screen does not remain open and the excel does not work after running it.

Sub LOGONSAP()

If Not IsObject(SAPguiApp) Then

    Set SAPguiApp = CreateObject("Sapgui.ScriptingCtrl.1")

End If

If Not IsObject(Connection) Then

    Set Connection = SAPguiApp.OpenConnection("MYSYSTEM", True)

End If

If Not IsObject(session) Then

   Set session = Connection.Children(0)

End If

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

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

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

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

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

End Sub

Accepted Solutions (0)

Answers (1)

Answers (1)

stefan_schnell
Active Contributor
0 Kudos

Hello Tony,

welcome in the Scripting Language Forum.

You can try this:

Sub LOGONSAP()

  Dim SAPguiAPP As SAPFEWSELib.GuiApplication

  Dim Connection As SAPFEWSELib.GuiConnection

  Dim Session As SAPFEWSELib.GuiSession

  If SAPguiAPP Is Nothing Then

    Set SAPguiAPP = CreateObject("Sapgui.ScriptingCtrl.1")

  End If

  If Connection Is Nothing Then

   'Set Connection = SAPguiApp.OpenConnection("MYSYSTEM", True)

   'Here I use a connection string, but it is equivalent to OpenConnection

    Set Connection = _

      SAPguiAPP.OpenConnectionByConnectionString("/H/10.100.200.300/S/3200", _

      True)

  End If

  If Session Is Nothing Then

    Set Session = Connection.Children(0)

  End If

  'Read user name from Excel table Sheet1, field A1

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

    Worksheets("Sheet1").Cells(1, 1).Value

  'Read password from Excel table Sheet1, field B1

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

    Worksheets("Sheet1").Cells(1, 2).Value

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

End Sub

To use this solution it is necessary to add at first the SAP GUI Scripting API library as reference to your VBA project from the Tools menu. How you can do that is descibed here.

Let us know your results.

Cheers

Stefan