C#使用 SAPscript的方法

主要參照文檔 
https://wenku.baidu.com/view/a28b71adcf22bcd126fff705cc17552707225ed2.html?_wkts_=1693296465595&bdQuery=AxSAPFEWSELib.AxGuiApplication.GetScriptingEngine
裏面講得很清楚,但是那個文檔中,最大的問題是代碼不能Copy,而且
1)首先必須安裝SAP GUI,當然SAP服務器,賬號,密碼這些基本條件沒問題,還有就是賬號開放了SAP Script功能
2)程序參考中必須引用2個com組件 SAP GUI Scripting API/ SAPRotWr 1.0 Type Library (找不到說明SAP GUI沒有正確安裝)
3)加using SAPFEWSELib;using SapROTWr;
4)登陸部分的代碼
 
private static object _lockObj = new object(); public GuiConnection Connection { get; set; } public GuiSession Session { get; set; } internal GuiApplication GetGuiSAPApp(int timeOut = 10) { CSapROTWrapper sapROTWrapper = new CSapROTWrapper(); return GetSAPGuiApp(sapROTWrapper, 10); } private GuiApplication GetSAPGuiApp(CSapROTWrapper sapROTWrapper,int secondsOfTimeOut) { object SapGuiRot = sapROTWrapper.GetROTEntry("SAPGUI"); if (secondsOfTimeOut < 0) { throw new TimeoutException("獲取SAPGUI Application超時時間不能小於0。"); } else { if (SapGuiRot == null) { System.Threading.Thread.Sleep(1000); return GetSAPGuiApp(sapROTWrapper, secondsOfTimeOut - 1); } else { object engine = SapGuiRot.GetType().InvokeMember("GetSCriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuiRot, null); if (engine == null) { throw new NullReferenceException("SAPGUI Application沒有發現。"); } return engine as GuiApplication; } } } public void OpenConnection(string server) { lock (_lockObj) { var Application = GetGuiSAPApp(10); try { Application.OpenConnectionByConnectionString(server); } catch(Exception ex) { throw new Exception("連接異常,查看端口或者Host是否正確。"+ex.Message); } var index = Application.Connections.Count - 1; Connection = Application.Children.ElementAt(index) as GuiConnection; index = Connection.Sessions.Count - 1; if (Connection.Sessions.Count == 0) { throw new Exception("新會話沒有發現,SAP客戶端是否開啓了腳本?"); } Session = Connection.Children.Item(index) as GuiSession; } } public bool Login(string UserName,string Password,string Client,string Language = "") { (Session.FindById("wnd[0]/usr/txtRSYST-BNAME") as GuiTextField).Text = UserName; (Session.FindById("wnd[0]/usr/pwdRSYST-BCODE") as GuiTextField).Text = Password; (Session.FindById("wnd[0]") as GuiFrameWindow).SendVKey(0); return true; }

 5)實際使用

    OpenConnection("server");  //server爲SAP地址或者服務器名
    Login("usreName", "Password", ""); //用戶名和密碼

 

6)其它部分可以參照login裏面的進行修改.

另外部分代碼: https://cloud.tencent.com/developer/ask/sof/1405501

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章