on 06-17-2016 10:55 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
88 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.