Home » excel » c# – Program runs properly from console, but not from Windows Service

c# – Program runs properly from console, but not from Windows Service

Posted by: admin May 14, 2020 Leave a comment


I have a .net program that interacts with Excel spreadsheets using the standard Microsoft.Office.Interop.Excel library. The program takes a file path as a command-line argument, and it runs fine on its own.

I also have a Windows Service that implements a file listener to detect changes in a certain directory. When a new file is detected, the Excel program is called along with the file path.

This execution starts off fine, but once the Excel program tries to open the file:

wb = excel.Workbooks.Open(FileName);

it crashes with the following error:

System.Runtime.InteropServices.COMException (0x800A03EC): Exception from HRESULT: 0x800A03EC
   at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)

Note that the code runs fine on its own, but when the program is called by the Windows service, it fails.

I’ve done plenty of searching for this issue, but none of the solutions I’ve found apply to my case.
Notably, I’ve tried changing the component services (according to this post), but there is no Excel component in my DCOM Config at all.

Any suggestions are greatly appreciated!

How to&Answers:

The error code 0x800A03EC (or -2146827284) means NAME_NOT_FOUND; in other words, you’ve asked for something, and Excel can’t find it.

Just a thought: are you trying to open an excel file relative to the installation path of the application? Keep in mind, when running a windows service, the working directory isn’t the same as where the application is installed.

Services are started from an application called Service Control Manager. This application lives in the system directory %WinDir%\System32

So, if you start your application from console, the working directory is the same as the directory where your application is installed. If the excel file is a local file, or relative to the installation path of the application, it will run just fine. However, when running as a windows service, it will look for the file in, or in a relative path from the %WinDir%\System32 directory.

If the above is the case, try using an absolute path (preferably configurable in the appsettings for example) or set the working directory properly.