I have the following C# function in my project, which is supposed to open and return an existing Excel workbook object:
Application _excelApp;
// ...
private Workbook OpenXL(string path, string filename)
{
try
{
if (_excelApp == null)
{
_excelApp = new Application();
}
Workbook workBook = _excelApp.Workbooks.Open(path + filename, // Name
0, // Do not update links
true); // Open read-only
return workBook;
}
catch (Exception e)
{
_excelApp = null;
throw new ArgumentException("Error opening " + path + filename, e);
}
}
But when I run it with "C:\" and "scratch.xlsx", the Open() call throws the following error:
Microsoft Excel cannot access the file 'C:\scratch.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.
The file and path does exist: I have copied the path from the error message and pasted it into a command window and the file loads up in Excel. The file is not locked: Excel can open it fine but my program cannot, even straight after a reboot. I am not trying to save it, I am trying to open it so the last option is irrelevant.
I am at a loss to understand why this simple piece of code is not working. Any suggestions would be hugely appreciated.
[edit] I have now tried opening that file from my personal network drive (M:) and from a USB stick. All to no avail.
The application is actually a Windows service, running under the local system account and generating reports. It currently write CSV reports with no access problems whatsoever. I am now trying to get it to open an excel file as a template report and fill in various fields. It is when opening the Excel file that it fails. I am thinking that the administrator account option everyone is suggesting is a red herring since it can write CSV files wityh no problem. [/edit]
--- Alistair.
while if you want to open an excel sheet, you click the C folder and type the name of your file in the search bar which is at the top right hand corner of the page. then just click on the file.
Alternatively, you can use the Excel keyboard shortcut Ctrl + O. Double-click the file you want to open. If the file still won't open, click “Browse” and search for the file. At the bottom of the “Open” dialog box, click the small arrow next to the “Open button”, then choose “Open and Repair” from the context menu.
Step 1: Launch Microsoft Excel on your PC. Step 2: Head to the “files” section, and click “Export.” Step 3: In the section, select “change file type” and click on the file with the error. Step 4: Change the file extension and save the file.
I found the following page:
http://social.msdn.microsoft.com/Forums/en-US/b81a3c4e-62db-488b-af06-44421818ef91/excel-2007-automation-on-top-of-a-windows-server-2008-x64
Where it says that...
it’s not supported to automate office products UI less. It seems that Windows Server 2008 and Excel 2007 enforce the given statement.
The questioner then describes exactly the situation I am in with a Windows Service that cannot open an Excel file, although the same code in a command-line program has no problem.
The response advises to create the following folder:
Windows 2008 Server x64: C:\Windows\SysWOW64\config\systemprofile\Desktop
Windows 2008 Server x86: C:\Windows\System32\config\systemprofile\Desktop
I have tried this and it worked a treat! Can anyone explain why it is needed and any downsides?
Thanks,
--- Alistair.
Run the program as admin, the C:/ cannot be accessed by a program unless the user is running as admin. You can make your program prompt the user it must be run as admin by altering the ApplicationManifest: How do I force my .NET application to run as administrator?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With