4
votes

I have a problem with starting the Excel Application under a particular user.

I try to schedule this script (C#) through an application X (not Windows Task Scheduler. And this application will always use a service account to run services on the server). If I run the C# script in command prompt under the same user, it runs. Under the application X, which uses the exact same user, to initiate the C# script, it fails to open the Excel application (not sufficient permission?).

This script calls: app.Workbooks.Open(ExcelFileName,0,false,Type.missing....), yet it gives the following error:

Microsoft Excel cannot access the file "...". 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 tried all the methods that I found online to no avail.

  1. Create directory “C:\Windows\SysWOW64\config\systemprofile\Desktop” (for 64 bit Windows) or “C:\Windows\System32\config\systemprofile\Desktop” (for 32 bit Windows). Then Set full control permissions on Desktop directory above (for example in Win7 & IIS 7 & DefaultAppPool set permissions for user “IIS AppPool\DefaultAppPool”)
  2. Changed the DCOM config for the Microsoft Excel application to include this user for Local/Remote Launch and Access
  3. Enabled all macros in Excel and set the Trust Center.
  4. Add the user to have full control on all folders that contain the Excel file.
  5. Under DCOM config, Microsoft Excel Application, if I modify the Identity tab to check on "This User" and enter the username/password to let Excel always run under that user. Then the application runs perfectly. However, other users can't run the excel application on their own with the following error: "Cannot use object linking and embedding". If I check "Use the launching user", then Excel can't be launched. No errors in the logs or events anywhere to check.

Yet, still the same error. I think it's permission but I am not sure where and what to do for this to work.

Now, normally, when I run this excel report, I can double-click on the file and it'd automatically run, save the new parameters into the current file and generate a new excel file (with date attached to the file name). That means there is a change (save) to the original file.

I appreciate all your help!

3
I also mentioned that this is Windows Server 2008 R2 running Microsoft Excel 2010. Thanks!ntumi

3 Answers

7
votes

I found the problem is the layer of security in the server!

Creating the Desktop folder inside the C:\Windows\System32\config\systemprofile and giving the service account permission to access the desktop folder is not enough.

I modified the C:\Windows\System32\config (or C:\windows\SysWOW64\config) folder to allow permission to the service account in security tab.

Then I also had to set up the same permission for the sub folder C:\Windows\System32\config\systemprofile (or C:\windows\SysWOW64\config\systemprofile) for the service account.

This works!

6
votes

Microsoft Excel cannot access the file in Server 2012 Excel 2016.. checked various solutions online to create folder desktop in C:\Windows\SysWOW64\config\systemprofile\desktop.. didn't work for this but then i added a folder desktop in System32 and that definitely worked.

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

0
votes

I ran across another way you can get this error: when you try to save a file with an "illegal" name, such as one with whacks (forward slashes) in it.

For that reason, it would seem reasonable that the err msg would give that as one possible explanation of the problem, but...no!

Specifically, I was trying to save a file named C:\RoboReporter\ABUELITOS\20160524_1327\ABUELITOS - Fill Rate - 4\1\61910B10

The whacks (shown as backwhacks in the copied exception text, but seen as forwardwhacks when hovering over the value when debugging) were the cause of the discombobulation. Once I fixed that by replacing whacks with underscores:

filename = String.Format("{0}\\{1} - Fill Rate - {2}.xlsx", uniqueFolder, _unit, _begDate.ToShortDateString());
filename = filename.Replace("/", "_");

...all went swimmingly.