如何将C#连接到SAP GUI? [英] How do I connect C# to SAP GUI?
问题描述
美好的一天!
我在实习期间需要提供的项目之一是在SAP GUI中自动执行交易(特别是在财务模块下)。是否可以使用c#自动执行事务,如果是,将c#连接到SAP GUI的步骤是什么?
谢谢!
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!
推荐答案
对于那些想要自动化SAP GUI并且只能做的人所以通过VBA和VB.Net。以下是使用C#自动化SAP的简单解决方案:
首先添加两个引用:
SAPFEWSELib
SapROTWr < ----这个位于安装SAP的文件夹中。
然后添加使用语句:
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:
using SAPFEWSELib;
using SapROTWr;
添加完成后,添加以下是一个按钮:
Once those are added, add the following to a button:
//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]");
此示例与Visual Basic版本完全相同。
如果要在SAP中的文本字段中输入文本,必须将对象明确地转换为正确的变量类型
在CO02交易中输入订单号的示例如下:
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:
GuiTextField ordNum = (GuiTextField)session.FindById("wnd[0]/usr/ctxtCAUFVD-AUFNR");
ordNum.Text = textBox1.Text;
frame.SendVKey(0);
无法连接到GUI 。您可以连接SAP数据。
请查看我的过去回答 [ ^ ]。
You can't connect to GUI. You can connect with SAP data.
Please, see my past answer[^].
任何人都可以共享下面的dll文件
SAPFEWSELib
SapROTWr
can anyone share the below dll file
SAPFEWSELib
SapROTWr
这篇关于如何将C#连接到SAP GUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!