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)登陆部分的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<br>        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", ""); //用户名和密码

 在加一段MM03抓取某料号的毛重的代码

 

复制代码
        public string GetGweight()
        {
            //輸入Tcoe:MM03
            //session.findById("wnd[0]/tbar[0]/okcd").text = "mm03"
            //session.findById("wnd[0]").sendVKey 0
            (Session.FindById("wnd[0]/tbar[0]/okcd") as GuiOkCodeField).Text = "MM03";
            (Session.FindById("wnd[0]") as GuiFrameWindow).SendVKey(0);
            //輸入料號
            //session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").text = "610300116625"
            //session.findById("wnd[0]").sendVKey 0
            (Session.FindById("wnd[0]/usr/ctxtRMMG1-MATNR") as GuiCTextField).Text = "610300116625";
            (Session.FindById("wnd[0]") as GuiFrameWindow).SendVKey(0);
            //選擇View:Basic Data 1
            //session.findById("wnd[1]/usr/tblSAPLMGMMTC_VIEW").getAbsoluteRow(0).selected = true
            //session.findById("wnd[1]/tbar[0]/btn[0]").press
            (Session.FindById("wnd[1]/usr/tblSAPLMGMMTC_VIEW") as GuiTableControl).GetAbsoluteRow(0).Selected = true;
            (Session.FindById("wnd[1]/tbar[0]/btn[0]") as GuiButton).Press();
            //取值
            //session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP01/ssubTABFRA1:SAPLMGMM:2004/subSUB4:SAPLMGD1:2007/txtMARA-BRGEW").setFocus
            string tmp =(Session.FindById("wnd[0]/usr/tabsTABSPR1/tabpSP01/ssubTABFRA1:SAPLMGMM:2004/subSUB4:SAPLMGD1:2007/txtMARA-BRGEW") as GuiTextField).Text;
            //回到主界面,方便下次操作
            (Session.FindById("wnd[0]/tbar[0]/okcd") as GuiOkCodeField).Text = "/n";
            (Session.FindById("wnd[0]") as GuiFrameWindow).SendVKey(0);               
          
            return tmp;
        }
复制代码

 

 6)其它Sapscript可以参照login里面的进行修改.

 7)限制条件: 没有登录的时候,SAP GUI是没有时间限制的。登录之后,会存在如果长时间不动,SAP GUI会断session,

可以联系SAP Basic顾问,看看能不能对这个账号不限制时间。否则就要间隔一段时间要动一下了。

我们自己也需要注意每次做完动作之后回到初始界面。或者tcode加"/n".

其它参考文件: https://cloud.tencent.com/developer/ask/sof/1405501 

 各种类型的说明可以参照下面的文档说明,这个文档特别详细.

https://www.synactive.com/download/sap%20gui%20scripting/sap%20gui%20scripting%20api.pdf

posted @   老飞飞  阅读(454)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2018-08-29 关于Table.Compute("Sum(qty)","XXX")的坑
点击右上角即可分享
微信分享提示