首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用c#自动执行SAP

如何使用c#自动执行SAP
EN

Stack Overflow用户
提问于 2012-10-30 05:50:03
回答 1查看 47.4K关注 0票数 24

我想使用C#语言自动化一个SAP窗口。我可以在VBScript中做到这一点,但是代码重用太可怕了。此外,我喜欢使用线程,而不是有80个或更多的进程运行。我在哪里可以找到如何做到这一点的文档和示例?这是我正在使用的代码。基本上,我面临的问题是-我如何与SAP GUI建立连接,然后动态创建SAP GUI,然后开始进行事务处理并在某些字段中输入文本。

代码语言:javascript
复制
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using White.Core.Factory;
using White.Core.UIItems.Finders;
using White.Core.InputDevices;
using System.Threading;
using System.Diagnostics;
using SAP.Connector;
using SAP;


namespace SAP_Automation
{
    class Program
    {
        public static void Main(string[] args)
        {
            string ExeSourceFile = @"C:\Program Files\SAP\SapSetup\setup\SAL\SapLogon.s8l";
            White.Core.Application _application;
            White.Core.UIItems.WindowItems.Window _mainWindow;

            var c = SAP.Connector.Connection.GetConnection("**");
            var c = new SAPConnection("ASHOST=*; GWHOST=*; GWSERV=*; ASHOST=*; SYSNR=00;USER=user; PASSWD=**;");
            c.Open();


            }
        }
    }
}

正如您所看到的,我可以创建一个连接,但我不知道如何创建到GUI的会话并开始在字段中输入文本。任何例子和样本将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-08 06:51:54

这可能是necro-threading,但我在工作中遇到了类似的情况。我们需要用于测试目的,它可以与我们用C#编写的自主开发的自动化平台的其余部分集成。我帮助创建了一个解决方案的提案,该方案利用了SAP提供的用于GUI自动化的库,该库可以用作SAP自动化层的基础。

您的SAP文件安装中是否存在以下文件?x:\Program Files\SAP\FrontEnd\SAPGui\sapfewse.ocx?

如果是这样,请将其作为引用添加到Visual Studio (或您正在使用的任何IDE )中。它基本上是一个类库,其中包含一堆特定于SAP的对象,这些对象允许您与进行交互。它非常有效,因为它公开了SAP GUI中所需的大部分内容。在其他尝试中,我们发现SAP中的许多对象都不可用。

这是我做的一个早期的概念证明。使用连接字符串启动SAP,输入凭据,导航到交易码。

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using SAPFEWSELib;

namespace SAPGuiAutomated
{
//created a class for the SAP app, connection, and session objects as well as for common methods. 
    public class SAPActive
    {
        public static GuiApplication SapGuiApp { get; set; }
        public static GuiConnection SapConnection { get; set; }
        public static GuiSession SapSession { get; set; }

        public static void openSap(string env)
        {
            SAPActive.SapGuiApp = new GuiApplication();

            string connectString = null;
            if (env.ToUpper().Equals("DEFAULT"))
            {
                connectString = "1.0 Test ERP (DEFAULT)";
            }
            else
            {
                connectString = env;
            }
            SAPActive.SapConnection = SAPActive.SapGuiApp.OpenConnection(connectString, Sync: true); //creates connection
            SAPActive.SapSession = (GuiSession)SAPActive.SapConnection.Sessions.Item(0); //creates the Gui session off the connection you made
        }

        public void login(string myclient, string mylogin, string mypass, string mylang)
        {
            GuiTextField client  = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-MANDT", "GuiTextField");
            GuiTextField login  = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-BNAME", "GuiTextField");
            GuiTextField pass  = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-BCODE", "GuiPasswordField");
            GuiTextField language  = (GuiTextField)SAPActive.SapSession.ActiveWindow.FindByName("RSYST-LANGU", "GuiTextField");

            client.SetFocus();
            client.text = myclient;
            login.SetFocus();
            login.Text = mylogin;
            pass.SetFocus();
            pass.Text = mypass;
            language.SetFocus();
            language.Text = mylang; 

            //Press the green checkmark button which is about the same as the enter key 
            GuiButton btn = (GuiButton)SapSession.FindById("/app/con[0]/ses[0]/wnd[0]/tbar[0]/btn[0]");
            btn.SetFocus(); 
            btn.Press();

        }
    }
    //--------------------------//
    //main method somewhere else 
    public static void Main(string[] args)
    {
        SAPActive.openSAP("my connection string");
        SAPActive.login("10", "jdoe", "password", "EN");
        SAPActive.SapSession.StartTransaction("VA03");
    }

你说得对,关于这个主题的文档并不多。下面是一些帮助我入门的资源

我们计划的-Original源代码http://scn.sap.com/thread/1729689

-Documentation on the API (适用于VB和javascript,但一般规则和对象是相同的)。请务必阅读SAP GUI运行时层次结构中的部分。它会回答很多问题。http://www.synactive.com/download/sap%20gui%20scripting/sap%20gui%20scripting%20api.pdf

票数 28
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13130176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档