且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将C#连接到SAP GUI?

更新时间:2022-06-20 09:04:45

对于那些想要自动化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