423,832 Members | 1,902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,832 IT Pros & Developers. It's quick & easy.

Windows Service accessing Excel file :(

emibt08
P: 25
Hello fellow programmers.
I am working on a project that is a windows service. It should open an excel file and read it and it worked good on WinXP SP2 w/ Office 2003. Now the customer upgraded to Windows Vista 32-bit w/ Office 2007 and keep getting the following error:
--------------------------------------------------------------------------------------------------
SOURCE: Microsoft Office Excel
MESSAGE: Microsoft Office Excel cannot access the file 'C:\ProgramData\DataProcApplication\DataFile.xls'. There are several possible reasons:

The file name or path does not exist.
The file is being used by another program.
The workbook you are trying to save has the same name as a currently open workbook.
--------------------------------------------------------------------------------------------------

i made sure the file exist and is in the correct folder. I gave admin privileges to the file and the folder, i run the service as admin and nothing seems to work. Now, i am sure the problem isn't in the file itself, since it's there. I also save some other binary files in that folder which the service can read/write to. As i assigned admin privileges, i can successfully read/write to a file in another path (not just isolated storage) like c:\somefile.txt.
So, i'm pretty sure the problem is with the excel interop, just cant really get it. This is the function that throws exception:

Expand|Select|Wrap|Line Numbers
  1. Workbook wb =
  2.                 excelApp.Workbooks.Open(
  3.                     path,
  4.                     2,      // UpdateLinks 1, 2, 3
  5.                     true,   // True to open the workbook in read-only mode.
  6.                     1,      // If Microsoft Excel is opening a text file, this argument specifies the delimiter character, as shown in the following table. If this argument is omitted, the current delimiter is used.
  7.                     null,   // Password string
  8.                     null,   // WriteResPassword string
  9.                     true,   // IgnoreReadOnlyRecommended
  10.                     XlPlatform.xlWindows,   // Origin
  11.                     ";",    // Delimiter
  12.                     false,  // Editable for add ins?
  13.                     false,  // Notify
  14.                     1,      // Converter ???
  15.                     false,  // AddToMru
  16.                     true,   // Local
  17.                     Microsoft.Office.Interop.Excel.XlCorruptLoad.xlNormalLoad); // CorruptLoad
  18.  
I've been trying to solve this all day long and trying to find a solution online with no luck. I saw posts with the same question though, but no answer. So, if anyone have a solution for this, please help me out.

Any help is greatly appreciated.
Cheers
Nov 17 '08 #1
Share this Question
Share on Google+
16 Replies


balabaster
Expert 100+
P: 797
I think the issue is likely that you are trying to open an Excel 2007 workbook using an older workbook object - i.e. from Excel 2003. It's not possible to open an xlsx file using the older Excel objects. You need to update your application references to point to the new Excel 2007 objects. In order to do this, the application will need to be compiled on a machine that either has Excel 2007 installed or has the correct DLL loaded onto it...
Nov 17 '08 #2

emibt08
P: 25
I think the issue is likely that you are trying to open an Excel 2007 workbook using an older workbook object - i.e. from Excel 2003. It's not possible to open an xlsx file using the older Excel objects. You need to update your application references to point to the new Excel 2007 objects. In order to do this, the application will need to be compiled on a machine that either has Excel 2007 installed or has the correct DLL loaded onto it...
Thanks for the reply balabaster, but that is not the problem. If you see the code, you can see that i am opening an excel 2003 workbook (xls file). However, i have updated the references to interop with Excel 2007, as i have Office 2007. Before the error started to show up, the service was running on Windows XP SP2 with Office 2003 with no problems and later on Windows XP SP3 with Office 2007. There were no problems with those systems. Once the OS was changed to Windows Vista 32-bit with Office 2007 the error came up. The references have been updated ever since the service was deployed at a system with Office 2007 and was working flawlessly. So, my guess is that it has something to do with Vista. However, as i said on internet i see the same question on many sites and forums. But i see no answer though :(
Nov 18 '08 #3

balabaster
Expert 100+
P: 797
Hmm... with this service, when you upgraded to Vista - did you make sure the service itself still has permission to interact with the desktop?
Nov 18 '08 #4

emibt08
P: 25
Hmm... with this service, when you upgraded to Vista - did you make sure the service itself still has permission to interact with the desktop?
Thanks for the reply. But again no luck. I haven't set that though and i did with checking that options in the Administration tools -> Services before messing with the registry and ServiceInstaller options. But i don't think it would help anyway since it's different in Vista. But i tried anyways. Then i created a console application that would do all of the Excel-related work. I called that app from another console and GUI app to check it out and everything if fine. But once i opened it from the service... again no luck. The same error message:

Exception: Microsoft Office Excel cannot access the file 'C:\TestExcelFiles\TestBook1.xls'. There are several possible reasons:

The file name or path does not exist.
The file is being used by another program.
The workbook you are trying to save has the same name as a currently open workbook.

Then i called that console app with CreateProcessAsUser to elevate some privileges and impersonate and again the same error (although i got a console window this time, which i don't need anyway).
So, i've been working on this issue for about 2-h straight and i'll keep up until it's solved, it just drives me nuts.

Please, if anyone has any suggestion about this post me.
Nov 18 '08 #5

P: 4
Any resolution on this? I am having the same problem.
Jan 8 '09 #6

P: 2
Wondering if you have solved this issue yet.

Unsure of vista specifically... but my research shows that automation is an issue with Windows Server 2008, specifically the 64-bit version.
The 32bit version works, as well as windows 2003 32/64bit.
I'm unsure as to whether it affects only SERVICES or APPLICATION automation as well..
We use a service for our tasks.

Recently we have run into this problem, and it's pretty important. We do some high dollar work using excel automation, and have recently obtained a new system running windows server 2008, just to find out there seems to be a bug/issue that prevents our application from doing its core work.
We get the same issue... excel loads, you go to open a workbook, and it errors out. Would appear to be a security / permissions bug, but have tried running under numerous accounts that have access to the file, and nothing.

If you have found a solution please let me know.
Jan 9 '09 #7

Expert 100+
P: 750
PRR
Try the following
1. Install your service using latest version of installutil..."\WINDOWS\Microsoft.NET\Framework\ve rsion"
2. Try installing the following ....Download details: 2007 Microsoft Office System Update: Redistributable Primary Interop Assemblies
3. Check on "MasterEvilAce" tip .. "my research shows that automation is an issue with Windows Server 2008, specifically the 64-bit version"
4. Finally ...Office automation is not recommended.....MS Office requires active user and start as interactive process... Also see if the office components are "initialized" when you are tryin to automate opening excel file... There may be some dependency.... Make sure some user has already logged in?
Also check on vista service account : session 0 thing....from what i know vista services run under Session 0.... session 0 does not have access to graphics or UI....
Also try explicitly giving file system rights n other rights to your service....

Let us know if you find the solution .....
Jan 10 '09 #8

P: 4
vote down


I have "kind of" solved this problem. If you use "dcomcnfg" to change the account to launch the excel application to a user account or "interactive user" the excel launched from the service starts in that account and it now can access the file system.

The problem is this is a global setting. If you use: * "this User": then excel ALWAYS opens with that user AND always opens up with no UI in session 0, even for interactive use of excel. IE its always invisible. * "Interactive User": Now excel always launches in the interactive user accout which I don't think will work for a service which will launch on bootup with NO interactive user.

There must be some security setting on this. I hope others have other ideas.
Jan 10 '09 #9

emibt08
P: 25
I kind of get a little frustrated with this kind of problem while working on a solution. Besides, there was not really any pointer to the right direction, and i needed the solution fast. So, i still believe it's a Vista issue, just didn't have enough time to spend on this issue.
However, this is how i solved it and made it work. I made another small application which would do all Office (read Excel) related stuff. Then, i spawn that application in the isolated storage of my service and communicate to it through named pipes (although i could use other methods of IPC). And then i just pass parameters to it and get results from it. So, the problem is definitely related to the service, not the user not having enough permissions etc. And since the service is starting/stopping the other application, it runs under the same user account as the service, just it's not a service. However, the issue might be due to the fact pointed out by DeepBlue, that the services in Vista run under session 0.
However, i don't exclude the possibility that it might be another M$ bug. Just need some time to do more research on the subject, but with the everchanging security models in Windows i don't even know if it's worth it. And then, is it gonna run on Windows 7, or maybe when it comes out and my customers upgrade will call to let me know that it again stopped working. It's just a real mess. Sometimes i wish that the Raymond Chen camp have won the battle inside M$. At least we would have the backward compatibility.

Cheers
Jan 20 '09 #10

P: 1
emibt08, I've been attempting to get this working for awhile now wondering what you mean by:

@emibt08
Where it is my understanding that the 'isolated storage' is a persistent storage medium for storing user configuration information... do you just mean that you spawn the application from within the service? or am I missing something?

Thanks, I'm getting a lot of heat to get this working

Matt
May 19 '09 #11

P: 1
I found another solution on this site:
http://social.msdn.microsoft.com/For...?prof=required

For 64-bit (x64), create this folder:
C:\Windows\SysWOW64\config\systemprofile\Desktop

For 32-bit (x86), create this folder:
C:\Windows\System32\config\systemprofile\Desktop

Personally, I had the problem on a VISTA x86 system. I created the folder as described in the 32-bit option above, and it solved my problem.

The windows service, if running under the systemprofile, needs the Desktop folder. This folder was automatically created on XP and older Windows Server versions, but not for Vista and Windows 2008 Server.
Mar 2 '10 #12

P: 1
csharper66 - that worked like a charm. i'm on win 7 x64 and have been struggling with this. thank you!!!!
Sep 21 '10 #13

P: 43
If you just need to read the file why do the automation? You could just just oledb jet engine provider to read the file. If you need to create then use an xslt.
Sep 22 '10 #14

P: n/a
csharper66 - Thanks. I agree with H Ogawa that it is safer than "registry hack".
Nov 10 '10 #15

P: n/a
You can only upload open files from C:\Windows\System32\config\systemprofile\Desktop
If the directory does not exist create it.
Saves changing the registry.
You can add sub dirs..
Nov 11 '10 #16

P: 1
That's great

It's work for me, thanks.

I was lost my hair
Oct 26 '18 #17

Post your reply

Sign in to post your reply or Sign up for a free account.