Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Good Day!

One of the projects that I need to deliver in my internship is to automate a transaction in SAP GUI(specifically under the financials module). Is it possible to automate a transaction by using c#, if yes what would be the steps for connecting c# to SAP GUI?

Thanks!
Posted
Updated 24-Sep-18 3:02am

For those who want to Automate the SAP GUI and are only able to do so through VBA and VB.Net. Here is a simple solution to automate SAP using C#:

First add two references:
SAPFEWSELib
SapROTWr <---- This one is located in the folder where SAP was installed.

Then add the using statements:

C#
using SAPFEWSELib;
using SapROTWr;


Once those are added, add the following to a button:

C#
//Get the Windows Running Object Table
SapROTWr.CSapROTWrapper sapROTWrapper = new SapROTWr.CSapROTWrapper();
//Get the ROT Entry for the SAP Gui to connect to the COM
object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI");
//Get the reference to the Scripting Engine
object engine = SapGuilRot.GetType().InvokeMember("GetScriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null);
//Get the reference to the running SAP Application Window
GuiApplication GuiApp = (GuiApplication)engine;
//Get the reference to the first open connection
GuiConnection connection = (GuiConnection)GuiApp.Connections.ElementAt(0);
//get the first available session
GuiSession session = (GuiSession)connection.Children.ElementAt(0);
//Get the reference to the main "Frame" in which to send virtual key commands
GuiFrameWindow frame = (GuiFrameWindow)session.FindById("wnd[0]");



This example works exactly like the Visual Basic Version does.


If you want to enter text into a text field within SAP, you must explicitly cast the object into the correct variable type

an example of entering an order number into CO02 Transaction, would be:

C#
GuiTextField ordNum = (GuiTextField)session.FindById("wnd[0]/usr/ctxtCAUFVD-AUFNR");
ordNum.Text = textBox1.Text;
frame.SendVKey(0);
 
Share this answer
 
v3
Comments
Dave Kreskowiak 24-Jun-16 12:24pm    
Don't drop a new answer on an old question (this one is a year and half old!). If you've got something useful like this, write up a tip/trick or even an article if you can go into enough detail about the how's and why's.
JSimp81719 29-Jun-16 18:26pm    
Dave, you are absolutely right, I should. The problem is, this search came up as one of the top google searches, and since I have successfully found a way to establish a COM reference to SAP, I thought I would share this since there is close to nothing out there except for people who may have access to the .Net connector; which I do not. So, I am sorry for posting this on such an old post, but thought it may be useful for someone who stumbled upon it themselves. Or at least a solid answer for historical purposes. Thanks for your feedback though
Hari prakash R 28-Mar-17 0:13am    
i can't use FindById. When i use it i got the run time error like this.
-> The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)). Please me to solve the error
You can't connect to GUI. You can connect with SAP data.
Please, see my past answer[^].
 
Share this answer
 
Comments
tanya de la merced 16-Oct-14 3:42am    
Thank you Sir!
Maciej Los 16-Oct-14 3:43am    
You're very welcome ;)
tanya de la merced 16-Oct-14 3:46am    
Sir if you dont mind, may i get your email? i have a lot of questions regarding sap and c#. Thank you sir:)
Maciej Los 16-Oct-14 3:54am    
If you have any question, please use this forum to ask it.
This is the best way to get an answer. The power is in CP community, not in single man :)
tanya de la merced 16-Oct-14 4:00am    
Thank you sir!:) I tried executing the program stated in the link, sad to say I can't download the sap connector
can anyone share the below dll file

SAPFEWSELib
SapROTWr
 
Share this answer
 
Comments
Sergio Sabas 21-Sep-18 0:21am    
In Visual Studio, open a NuGet Package Manager Console and type the following:

Install-Package SAP.GUI.Scripting.net -Version 1.0.0

That will install the DLLS on your current solution. Hope this helps.
Richard Deeming 25-Sep-18 11:58am    
If you want to ask a question about a solution, click the "Have a Question or Comment?" button under that solution.

DO NOT post your comment as a new "solution".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900