Question: How to open a new SAP GuiApplication targeting SAP Logon instead of SAP Easy Access?
I know this seems like a repeat question however, it is more an addition to the question How do I automate SAP GUI with c#. Due to my lack of reputation points, I am unable to ask a question in the comments section so please forgive me.
I was able to successfully implement a connection to SAP using C# however, it is connecting to "SAP Easy Access" instead of "SAP Logon" -A more visually appealing GUI. I do not necessarily care about the visual appearance; the issue is certain buttons or text input fields are unavailable or not visible. Here is a picture:
I believe the issue is with the DLL library or me not knowing how to start an instance of SAP Logon and attach it to my session.
Here is my code excluding sensitive data:
using System;
using SAPFEWSELib;
namespace RS_PostSettlementDataPull
{
//SAPActive class to perform script operations
public class SAPActive
{
//Class Member accessors
public static GuiApplication SapGuiApp { get; set; }
public static GuiConnection SapConnection { get; set; }
public static GuiSession SapSession { get; set; }
/**
* Takes in a connection string and creates a GuiApplication linking
* the SAP Connection and Session.
*
* Accepts DEFAULT connection.
*/
public static void OpenSap(string env)
{
SAPActive.SapGuiApp = new GuiApplication(); //Need a way to explicitly create SAP Logon app
string connectString = null;
if (env.ToUpper().Equals("DEFAULT"))
{
connectString = "1.0 Test ERP (DEFAULT)";
}
else
{
connectString = env;
}
SAPActive.SapConnection = SAPActive.SapGuiApp.OpenConnection(connectString, Sync: true); //creates connection
SAPActive.SapSession = (GuiSession)SAPActive.SapConnection.Sessions.Item(0); //creates the Gui session off the connection you made
}
/**
* Takes in connection parameters to login to the SAP session.
*/
public static void login(string myclient, string mylogin, string mypass, string mylang)
{
GuiTextField client = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-MANDT", "GuiTextField");
GuiTextField login = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-BNAME", "GuiTextField");
GuiTextField pass = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-BCODE", "GuiPasswordField");
GuiTextField language = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-LANGU", "GuiTextField");
client.SetFocus();
client.Text = myclient;
login.SetFocus();
login.Text = mylogin;
pass.SetFocus();
pass.Text = mypass;
language.SetFocus();
language.Text = mylang;
//Press the green checkmark button which is about the same as the enter key
GuiButton btn = (GuiButton)SapSession.FindById("/app/con[0]/ses[0]/wnd[0]/tbar[0]/btn[0]");
btn.SetFocus();
btn.Press();
}
/**
* Takes in a string UI path Id and returns a GuiTextField linked to the
* current session. Utilizes the SAPActive class.
*/
public static GuiTextField TextFieldPath(string path)
{
GuiTextField rtnField = (GuiTextField)SAPActive.SapSession.FindById(path);
return rtnField;
}
/**
* Takes in a string UI path Id and returns a GuiMenu linked to the
* current session. Utilizes the SAPActive class.
*/
public static GuiMenu MenuPath(string path)
{
GuiMenu rtnField = (GuiMenu)SAPActive.SapSession.FindById(path);
return rtnField;
}
/**
* Takes in a string UI path Id and returns a GuiFrameWindow linked to the
* current session. Utilizes the SAPActive class.
*/
public static GuiFrameWindow FrameWindowPath(string path)
{
GuiFrameWindow rtnField = (GuiFrameWindow)SAPActive.SapSession.FindById(path);
return rtnField;
}
/**
* Takes in a string UI path Id and returns a GuiButton linked to the
* current session. Utilizes the SAPActive class.
*/
public static GuiButton ButtonPath(string path)
{
GuiButton rtnField = (GuiButton)SAPActive.SapSession.FindById(path);
return rtnField;
}
/**
* Main class that acts as the driving SAP GUI Script engine.
*/
public static void Main(string[] args)
{
SAPActive.OpenSap("SENSITIVE");
SAPActive.login("100", "SENSITIVE", "SENSITIVE", "EN");
SAPActive.SapSession.StartTransaction("IW59");
GuiMenu guiMenu;
GuiFrameWindow guiFrameWindow;
GuiTextField guiTextField;
GuiButton guiButton;
guiMenu = SAPActive.MenuPath("wnd[0]/mbar/menu[2]/menu[0]/menu[0]");
guiMenu.Select();
guiTextField = SAPActive.TextFieldPath("wnd[1]/usr/txtV-LOW");
guiTextField.SetFocus();
guiTextField.Text = "SENSITIVE";
guiTextField = SAPActive.TextFieldPath("wnd[1]/usr/txtENAME-LOW");
guiTextField.Text = "";
guiFrameWindow = SAPActive.FrameWindowPath("wnd[1]");
guiFrameWindow.SendVKey(8);
guiFrameWindow = SAPActive.FrameWindowPath("wnd[0]");
guiFrameWindow.SendVKey(8);
//guiButton = SAPActive.ButtonPath("wnd[1]/tbar[0]/btn[8]");
//guiButton.SetFocus();
//guiButton.Press();
}
}
}
Thank you for any assistance you are able to provide!