cancel
Showing results for 
Search instead for 
Did you mean: 

Opening SAP and login in using a VBS

0 Kudos

Dear reader,

I am just starting to use macro's inside SAP and I am now trying to convert it to VBS.

The code I am currently using doesnt open my SAP.

Can someone take a look at it and tell me what I am doing wrong.

Thank you in advance.

Dim SapGui
Dim SAPApp
Dim connection
Dim session
Dim WSHShell
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus       'You need to fill there path for SAP Logon exe file
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 SAPApp = SapGui.GetScriptingEngine
Set connection = SAPApp.OpenConnection("5.1.1 AP1 ERP Production", True)     'You need to fill there name of system ID
Set session = connection.Children(0)
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "500"      'Client
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "Name"  'User Name
session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "Password"   'Password
Execute
session.findById("wnd[0]").sendVKey 0

Kind Regards,

Teddy

Accepted Solutions (0)

Answers (4)

Answers (4)

bhaskar_basam
Contributor

Please follow Csongor Varga in Youtube. He has attached excel sheets which integrate with SAP which are already available in the videos. Include your above requirement

0 Kudos

Thanks for the answers, unfortunately it still isn't an answer we can work with...

Sriram2009
Active Contributor
0 Kudos

Hi Teddy.

Refer the SAP note https://launchpad.support.sap.com/#/notes/103019 to execute the SAPGUI using command prompt(either bat or windows CMD file).

Regards

Sriram

Jhefferson
Newcomer
0 Kudos

Hello Teddy,

I noticed you're encountering issues with running macros within SAP and converting them to VBS. I've faced similar challenges in the past and would like to offer my assistance.

Firstly, I'd like to share a code snippet that I've developed which may help resolve your issue:

vbscript
Copy code
Option Explicit

Sub SAPLogon_VBScript()

' Declarando variáveis --------------------------------------------------------------------------------------------

Dim SAPGui Dim App
Dim Connection
Dim Session

Dim wshshell

' Inicializando o ----------------------------------------------------------------------------------------

de logon do SAPShell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus
Set wshshell = CreateObject("WScript.shell")

' Aguardando que o sistema SAP abra ---------------------------------------------------------------------------------

Do Until wshshell. AppActivate("SAP Logon")Application.Wait Now + TimeValue("0:00:02")Loop Set wshshell = Nothing

' Conectando-se ao SAP para login ------------------------------------------------------------------------------------

Set SAPGui = GetObject("SAPGUI")


Set App = SAPGui.GetScriptingEngine

Set Connection = App.OpenConnection("ECC - Desenvolvimento Remoto
Environment", True)Set Session = Connection.Children(0)


' Inserindo dados de acesso ------------------------------------------------------------------------------------------

Session.findById("wnd[0]/usr/txtRSYST-MANDT"). Text = "400" '- Seu código
de cliente Session.findById("wnd[0]/usr/txtRSYST-BNAME"). Text = "username" ' Seu nome
de usuário Session.findById("wnd[0]/usr/pwdRSYST-BCODE"). Text = "password" ' Sua senha
Session.findById("wnd[0]/usr/txtRSYST-LANGU"). Text = "EN" ' Seu idioma
Session.findById("wnd[0]/tbar[0]/btn[0]").press

' Seu código abaixo ------------------------------------------------------------------------------------------------

Session.findById("wnd[0]/tbar[0]/okcd"). Text = "/N"Session.findById("wnd[0]"
).sendVKey 0

'...

' Terminando a conexão com o sistema SAP ------------------------------------------------------------------------------

Set Session = Nothing Set Connection = Nothing Set App = Nothing Set SAPGui = Nothing




End Sub
Sinta-se à vontade para testar este código e me avise se você encontrar algum problema ou precisar de mais assistência.

Atenciosamente,Jhefferson