如何从运行Windows服务的Excel VBA代码 [英] How to run excel vba code from a Windows Service

查看:139
本文介绍了如何从运行Windows服务的Excel VBA代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要运行一些从我的Windows服务练成VBA代码。该服务使用的是FileSystemWatcher的监视目录添加XML文件。一旦文件被添加XML文件的内容进行反序列化到对象属性。在这个阶段,我想打开一个Excel文件,并在某些细胞中传递这些价值和运行的工作簿的VBA代码。我有这个完全从Windows窗体应用程序工作,但我不能让它从我的Windows服务应用程序的工作。我附加了调试器应用程序尝试,看看发生了什么事,但没有引发的错误,并全部顺利完成的步骤。我知道,一个Windows服务不支持打开MS Office文件,因为是与用户界面和它具有的用户权限进行交互的问题。但是,我要寻找一个变通的办法能够从一个服务运行此VBA代码。我使用Windows 7家庭高级版32位和我有我对我的服务设置为LocalSystem.This帐户是我使用的代码:

I want to run some excel vba code from my Windows Service. The Service is using a fileSystemWatcher to monitor a directory for an xml file to be added. Once a file is added the content of the xml file is deserialized into object properties. At this stage I want to open an excel file and pass these values in to certain cells and run vba code from that work book. I have this working perfectly from a windows forms application but I cant get it working from my Windows Service application. I attached a debugger to the application to try and see what was happening but there were no errors thrown and all the steps completed successfully. I know a Windows Service does not support opening MS Office files as there is issues with interacting with the UI and the User permissions it has. But I am looking for a work around any way to be able to get this vba code running from a service. I am using Windows 7 Home Premium 32 bit and I have my Account for my service set to LocalSystem.This is the code that I am using:

    private void FSWatcherTest_Created(object sender, System.IO.FileSystemEventArgs e)
    {
            Trade t;
            XmlSerializer serializer;
            XmlReader reader;
            XmlWriter writer;

            string filePath = @"C:\Inbox\TradeInfo.xml";


            serializer = new XmlSerializer(typeof(Trade));
            reader = XmlReader.Create(filePath);
            t = (Trade)serializer.Deserialize(reader);
            reader.Close();



            string path=@"C:\Windows\System32\config\systemprofile\Desktop\TwsDde.xls";

            oXL = new Microsoft.Office.Interop.Excel.Application();

            oXL.Visible = true;

            oXL.DisplayAlerts = false;

            mWorkBook = oXL.Workbooks.Open(path,2, false, 5, "", "", true,Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true,false, false);

            //Get all the sheets in the workbook

            mWorkSheets = mWorkBook.Worksheets;

            //Get the allready exists sheet

            mWSheet1=(Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item("Basic Orders");

            // Microsoft.Office.Interop.Excel.Range range= mWSheet1.UsedRange;

            mWSheet1.Cells[12, 1] = "GE";
            mWSheet1.Cells[12, 2] = "STK";
            mWSheet1.Cells[12, 7] = "SMART";
            mWSheet1.Cells[12, 9] = "USD";
            mWSheet1.Cells[12, 12] = "Buy";
            mWSheet1.Cells[12, 13] = "100";
            mWSheet1.Cells[12, 14] = "MKT";

            Excel.Range range;
            Excel.Range row;

            range = mWSheet1.get_Range("A12", "O12");
            range.EntireRow.Select();

            oXL.Run("TwsDde.xls!Sheet2.placeOrder");
        }



任何帮助将不胜感激。或做同样的事情,即运行包含此代码可能是Windows窗体的替代方式?

Any help would be greatly appreciated. Or alternative ways of doing the same thing i.e running a windows form that contains this code maybe?

推荐答案

您好我设法得到这个在一种四围的方式工作。我基本上遵循本教程和代码上的从服务推出一个互动的过程,从而使服务作为SYSTEM用户运行,但采取的第一个登录用户的会话ID,并给用户管理员权限才能运行的互动过程,如GUI应用程序。要打开Excel,并通过Windows服务我第一次写所有的代码要做到这一点在Windows运行宏窗体应用程序和测试它在那里工作的第一位。然后,我改变了可执行文件,它完美地工作。我希望这可以帮助其他人同样的问题。

Hi I managed to get this working in a kind of round about way. I basically followed this tutorial and code on launch an interactive process from a service to allow the service to run as SYSTEM user but to adopt the session ID for the first logged on user and give the user Administrative privileges to run interactive processes such as GUI applications. To open excel and run a macro through a Windows service I first wrote all the code to do this in a Windows Forms application and tested it worked there first. I then changed the executable and it worked perfectly. I hope this helps someone else with the same problem.

这篇关于如何从运行Windows服务的Excel VBA代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆