Reputation: 235
I found the following code in Stack Overflow and used it, and I was able to bring up SAP GUI. However, the issue is that it just gets stuck on the SAP Logon 740 Screen
which is not correct because I am already logged into SAP GUI before running the VBA code. When the VBA code runs, it should start from the SAP Easy Access Screen
and run my SAP GUI recorded script. SAP does not run on virtual machine.
Sub testing()
Dim Appl As Object
Dim Connection As Object
Dim session As Object
Dim WshShell As Object
Dim SapGui As Object
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", 4
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 Appl = SapGui.GetScriptingEngine
Set Connection = Appl.Openconnection("01. PRD - ERP Production", True)
Set session = Connection.Children(0)
If session.Children.Count > 1 Then
answer = MsgBox("You've got opened SAP already, please leave and try again", vbOKOnly, "Opened SAP")
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT3").Select
session.findById("wnd[1]/usr/radMULTI_LOGON_OPT3").SetFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
Exit Sub
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").selectNode = "F00004"
session.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode = "F00004"
MsgBox "Script Complete"
End Sub
Upvotes: 2
Views: 1208
Reputation: 13638
When you say "running from SAP Easy Access", I guess you want to start a program via a Transaction Code, whatever the current screen is. To do this, a user would type /N
followed by the Transaction Code in the Command Field, e.g. to start the transaction code FB01
here:
With SAP GUI Scripting, you can do this:
session.StartTransaction "FB01"
More information about StartTransaction
: GuiSession Object
Upvotes: 1