1

Morning,

I am writing simple windows service that will take excel file from given location, generate xml file and move excel to another folder. I am using:

Excel = Microsoft.Office.Interop.Excel

My code looks like this:

        Excel.Application xlApp = null;
        Excel.Workbook xlWorkBook = null;
        Excel.Worksheet xlWorkSheet = null;

        try
        {
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open(file);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item("Części");



            //doing something with xml and excel

            xlApp.DisplayAlerts = false; 
            xlWorkBook.Close();
            xlApp.Quit();
        } catch (Exception e) {
            this.writeErrorLog(e);
        } finally {
            // Close the Excel process
            if (null != xlWorkSheet)
                Marshal.ReleaseComObject(xlWorkSheet);
            if (null != xlWorkBook)
                Marshal.ReleaseComObject(xlWorkBook);
            Marshal.ReleaseComObject(xlApp);
            GC.Collect();
        }

My problem is that when i run it as normal program it works fine and does everything i need. But when i create service and install it, it creates exceptions every time i upload excel file to this folder. Exception says that program could not gain access to my excel file.

System.Runtime.InteropServices.COMException

I have checked this topic but it didn't help me. Can anyone help me solve this problem?

EDIT: Btw. I am using this tutorial to create service.

EDITv2: The way i take file paths:

string sc_path = @"C:\Projekty\AAPLXML\AppFolders\upload";
        string tg_path = @"C:\Projekty\AAPLXML\AppFolders\processed";

        if (System.IO.Directory.Exists(sc_path) && System.IO.Directory.Exists(tg_path))
        {
            List<string> files = System.IO.Directory.GetFiles(sc_path).ToList();
            if (files.Count == 0) return;
            files.ForEach(f => new XmlGenerator(f).Start());
        }
Community
  • 1
  • 1
  • Is the service running as your user account or the standard system? where is the excel file held? – BugFinder May 24 '16 at 09:17
  • Service is running as LocalSystem and file is held om my C drive in a folder. Later it is meant to be held on server. – Marcin Rejnowski May 24 '16 at 09:18
  • Is the folder accessible by system? – BugFinder May 24 '16 at 09:19
  • All of this folders are accessible by System. When the exception is thrown i tell my program to save error log and move excel to another folder and this is done properly. – Marcin Rejnowski May 24 '16 at 09:21
  • Add more logging - as to what exactly fails, the fact your excel is complaining suggests something is wrong. such as I have found that if you dont set all those xlapp, xlworkbook,xlworksheet to null, excel can think its still running.. or has access to these things even when it hasnt.. you need to determine exactly what failed as chances are its something in the // doing something stage.... – BugFinder May 24 '16 at 09:27
  • When i took Exception to String it says that it could not gain acces in 'Microsoft.Office.Interop.Excel.Workbooks.Open()' The problem is that i do not fully understand what parameters i should use to set this method right. – Marcin Rejnowski May 24 '16 at 09:32
  • then it doesnt have access to the file by looks of that.. are you sure you're providing full path? – BugFinder May 24 '16 at 09:35
  • Edited. The file has spaces in its name. the path that i pass to the open method looks like that: "C:\\Projekty\\AAPLXML\\AppFolders\\upload\\Arkusz zamówień (+RS200)-V1-v1.xlsm" – Marcin Rejnowski May 24 '16 at 09:43
  • Then it still sounds like it hasnt got the access you think it does. obvious choices are: excel hasnt been run as system, and so its not configured and its waiting on some screen, it doesnt have access to the file, the file is open by someone else.. what happens if you run the service as you? – BugFinder May 24 '16 at 09:54
  • [link](http://stackoverflow.com/questions/18484633/c-sharp-windows-service-com-exception-80080005-when-starting-application) Then i get error like that. But i also find [that](http://stackoverflow.com/questions/14037412/cannot-access-excel-file) so i am wondering if i should continue working or that or ain't it better to abbandon this concept. – Marcin Rejnowski May 24 '16 at 09:58
  • Yes you would need to change your account to be allowed to be a service account and so on.. however, it rules out a permissions thing with system.. and you can setup a functional account to run it as in the real world. Only you can decide if your stuff needs to be a service, a scheduled task, or whatever – BugFinder May 24 '16 at 10:00
  • Anyway man thanks for willing to help. If i will have any more problems i will edit my question. Thank you very much! – Marcin Rejnowski May 24 '16 at 10:04

0 Answers0