57

I have my code as follows :-

Microsoft.Office.Interop.Excel.Application oXL = null;
Microsoft.Office.Interop.Excel.Sheets sheets;
Application excel = new Microsoft.Office.Interop.Excel.Application();


excel.Workbooks.Add(System.Reflection.Missing.Value);

/*
    * Here is the complete detail's about Workbook.Open()
    * 
    *  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)
    */

Workbook workbook = excel.Workbooks.Open(
    System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Template.xlsx"),
    Missing.Value, true, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value);
sheets = workbook.Worksheets;

Now for the line :-

workbook = excel.Workbooks.Open(
    System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Template.xlsx"),
    Missing.Value, true, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value, Missing.Value, Missing.Value,
    Missing.Value);

It gets executed directly from the visual studio (F5) but when i try to access it with IIS it wont work. Throws error as follows:-

Microsoft Office Excel cannot access the file 'c:\inetpub\wwwroot\Timesheet\App_Data\Template.xlsx'. 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 have tried the work around as:-

  • Folder and the file exist, giving access to the IUSR_### (IIS user) and to the ASPNET user in the folder where the file is.
    • At Component Services(DCOM) given access to appropriate user.

I have already given all permission's to the folder where the template(.xlsx) exists

Any suggestions??

Shubh
  • 6,693
  • 9
  • 48
  • 83

9 Answers9

133

Try this:

  1. Create the directory

C:\Windows\SysWOW64\config\systemprofile\Desktop

(for the 32-bit version of Excel/Office on a 64-bit Windows computer) or

C:\Windows\System32\config\systemprofile\Desktop

(for a 32-bit version of Office on a 32-bit Windows computer or a 64-bit version of Office on a 64-bit Windows computer).

  1. For the Desktop directory, add Full control permissions for the relevant user (for example in Win7 & IIS 7 & DefaultAppPool set permissions for user IIS AppPool\DefaultAppPool).

Original post with answer:

John C
  • 3,052
  • 3
  • 34
  • 47
Eric Bonnot
  • 2,004
  • 1
  • 20
  • 22
  • 2
    should probably ack the original. (http://social.msdn.microsoft.com/Forums/en/innovateonoffice/thread/b81a3c4e-62db-488b-af06-44421818ef91) – Michael Dausmann Nov 25 '11 at 05:45
  • By the way, you can read this thread http://stackoverflow.com/questions/1041266/c-sharp-and-excel-automation-ending-the-running-instance which is really instructive if you want to use office interop. – Eric Bonnot Nov 30 '11 at 16:24
  • Creating the directory worked for me. I also added full control to system, not sure if needed. Thanks. – live-love Aug 31 '12 at 16:07
  • I added permission to NetworkService user, to Desktop folder and it works. Regards!! – Sebastián Sep 25 '12 at 21:11
  • 3
    Does anybody have an explanation for this behavior? – Florin Bombeanu Nov 20 '12 at 12:08
  • I'm interested by WHY IT'S WORKING!! but really thanks guys you saved my day (or my week) – Frederic Jun 12 '13 at 18:26
  • I was getting this error after moving to Windows 7. Did Microsoft forget to put it in this folder as part of Office installation on Windows 7? Are they really that dumb! Nonetheless, I am interested in finding out if that indeed was the case. – name_masked Mar 25 '14 at 18:03
  • thank you it helps me to fix the the same problem when scheduling a console app in windows scheduler . believe it or not just adding this folder solved the problem – Iman May 23 '15 at 10:20
  • Holy @#$!, this took me days to figure out. Problem solved, thanks mate! – devHead Jun 02 '15 at 16:14
  • This is also the way we solved our problem. Strangely, we have a production server running Windows Server 2008 R2 Enterprise that doesn't have the Desktop folders, and the application works like a charm. But on another server with Windows Server 2008 R2 Standard, it needed these two folders in order to work. I would be really happy to see an explanation of why two empty folders can solve this issue. – Apostrofix Jul 22 '15 at 06:22
  • Any idea what to do if it's a Linux server running samba? – Roger Dueck Apr 25 '16 at 22:04
  • 1
    Adding the System32 folder solved the problem on Win10 x64 21H2 with Office 2021 for a program using Microsoft.Office.Interop.Excel 15.0 with .Net 4.72, so the problem still persists. – avenmore Feb 17 '22 at 15:55
  • Same here, 10/2022... Adding the System32 folder solved the problem for Interop.Excel 15.0 .Net 4.8! – MX313 Oct 25 '22 at 15:51
19

Let me note that in my place, adding the c:\windows\syswow64\config\systemprofile\desktop directory didn't work.

The point is that WOW64 stands for Windows on Windows64, that means it actually applies for 32-bit programs running on the 64bit OS.

Since I have 64-bit Excel installed, the proper directory turned out to be the c:\windows\system32\config\systemprofile\desktop

Nikhil
  • 323
  • 3
  • 9
  • 1
    good callout!! i've been searching how to fix this problem for 2 days and this alternate directory fixed my my problem using windows server 2016 and 64 bit excel 2016 – Taylor Francis Feb 21 '20 at 19:31
  • 1
    My problem was fix by applying this approach. Thanks – Werdo Nov 10 '20 at 06:33
  • Same for me with excel 16.0 64bit on Windows 11, Added full control to my user on `C:\Windows\system32\config\systemprofile\Desktop` – Jonathan Larouche May 17 '23 at 16:01
9

In my case, I followed the suggestions provided here and solved the problem.

Steps:

  1. Run dcomcnfg
  2. Go to Console Root \Component Services\Computers\My Computer\DCOM Config\Microsoft Excel Application
  3. Right click Microsoft Excel Application
  4. Select Properties
  5. Go to Identity tab
  6. Select The interactive user.

In step 2, if you can't locate that path then try running mmc comexp.msc /32 instead of dcomcnfg.

datchung
  • 3,778
  • 1
  • 28
  • 29
  • My problem was fix by applying this approach. Thanks – milad shafiei Jul 27 '21 at 07:42
  • This is the only approach that solved the problem for me. Thank you! – Mohammad Anini Sep 06 '21 at 20:53
  • this worked for me, thanks a TON – Sunil Mathari Feb 10 '22 at 05:07
  • This worked for me as well (probably in combination with the directory solution and giving that desktop directory proper permission - since I did that first but still without success). Just want to point out that for me this doesn't work if I use ApplicationPoolId for identity in IIS, but works with either "Local System" or "Custom Account (windows user and pass)".. Also in the same window where you are in step 5, if you go to security, check that you have the right permissions there as well. – Spluf Feb 22 '22 at 12:18
1

I would like to add something to the Eric Bonnot's answer : The answer worked out partially as I worked on a IIS server with a non-admin user with the powerpoint PIA.

I noticed that I could not open a pptx file if this one had a media (picture for example) in it.

The "hack" was to add also the rights to the windows user (the one using the PIA) on the systemprofile/AppData directories.

Hope this helps

1

The following solution worked for me.

Create the directory C:\Windows\SysWOW64\config\systemprofile\Desktop

0

I was previously attempt this problem then i resolved.

Solution:

I put the full permission to particular folder(Sub folder and files) and checked working fine.

0

I've wrapped my WCF in a Windows Service. Creating the Desktop Folders did solve it for me on one machine, but not on another.

My problem in the end was, that my Windows Service did not run under an active User of the machine. Configuring the service to run under a user which is active on the machine solved this problem for me so far.

Only the combination of

  • Existing Desktop Folder
  • Service running under a real user Account

got it working for me.

This article lead me to the full solution: Cannot access excel file

Community
  • 1
  • 1
Florian Moser
  • 2,583
  • 1
  • 30
  • 40
0

In my case, none of the solutions given worked for me (changing user in dcom settings, creating desktop folder and giving user full access to it, etc).

I finally realized I had a 64-bit version of excel. Uninstalling the 64-bit version and installing the 32-bit version of excel solved my solution to this error. This was with using Windows 10 and Microsoft Office Excel 2010.

-1

This works

excel.exe /safe

This does not and gives the same error as the regular excel startup

excel.exe /automation

This also occurs for all MS Office 2007 apps for ANY network file. Local file access is fine.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
wbaz
  • 1