2

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

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • Why do you keep aksing similar questions [1](https://stackoverflow.com/questions/73967984/logging-into-sap-gui-first-and-then-running-vba-code) [2](https://stackoverflow.com/questions/73930705/excel-sap-automation-error-invalid-syntax) and posting similar code. I am afraid but if you are not able to improve your question it will be difficult to help. – Storax Oct 06 '22 at 18:12
  • What is so difficult about my question? You seem to be the only one who keeps voting down my question because you dont "understand" it. People from my previous post understood my question except for you. If you are unable to help, please stay away from my questions. – marsprogrammer Oct 06 '22 at 18:25
  • But also the other people did not answer your question to your satisfaction. You did not mark any answer as helpful. Anyway, as you wish I will stop commenting on your question. Good luck and farewell. – Storax Oct 06 '22 at 18:41
  • The script is misleading, it doesn't correspond to your question because it always starts a new connection (`OpenConnection`) and you then need to enter user and password first. – Sandra Rossi Oct 08 '22 at 11:37

1 Answers1

1

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:

enter image description here

With SAP GUI Scripting, you can do this:

session.StartTransaction "FB01"

More information about StartTransaction: GuiSession Object

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • So do I delete the loop as well ? – marsprogrammer Oct 08 '22 at 16:41
  • Your script is not related to your question. In Stack Overflow, questions must be focused, so better ask another question. See [Asking](https://stackoverflow.com/help/dont-ask) and other help pages about asking. – Sandra Rossi Oct 08 '22 at 17:07