Debugging/Troubleshooting Help
To be honest, I'm not sure, if the issue is with my code, or with some other inane Windows setting. I'm just hoping more seasoned eyes can possibly point me in the correct direction.
I have a relatively simple console app, which takes a directory as the parameter and then iterates through the spreadsheets located therein and refreshes all the data connections. If there are any errors, I'm now writing a text file with the exceptions, and I also generate an email. When I run this in debug mode within the IDE, everything works perfectly and the files are updated. I can look inside the directory and see the new time stamp on each file.
So, I thought I was done and compiled the app for release and installed it. I then set it up as a scheduled task on my laptop (its intended use), and ran into a few errors. I had to modify the properties for Excel in the Component Services applet for my alias, which enabled the task to run, but now I'm getting exceptions from Excel. I added a method to write out the exceptions to a text file, but I think that this error is misleading. The exception generated from the TRY block is below.
Microsoft Excel cannot access the file '*********.xlsm'. 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.
When I run the task, the files are in fact not in use. I can open them up directly, and I also don't see the Excel temp files, which would indicate that the file is in use by someone else.
I've added my alias to all the properties for Excel in the Component Services applet, but still no luck. Because this doesn't occur while debugging in VS, I'm not exactly sure now, how to narrow down the issue. The app is able to create the text file in the network share, and it is also able to generate an email from a mailbox I have permission to send from, so I don't believe it's a permission issue.
Any insight that would help pinpoint the root cause of this perplexing error would be greatly appreciated.
TIA,
Chris
--Here is the method that is failing.Code Snippet- private void _DataRefresh(String S_Directory)
- {
- if (S_Directory.Substring(S_Directory.Length - 1, 1) != @"\")
- {
- S_Directory = S_Directory + @"\";
- }
- DirectoryInfo finfo = new DirectoryInfo(S_Directory);
- foreach (FileInfo fileName in finfo.GetFiles("*.xls*"))
- {
- if (isOpen(S_Directory + fileName) && fileName.ToString().Substring(1, 2) != "~$")
- {
- try
- {
- xls.Application ExcelObj = new xls.Application();
- ExcelObj.DisplayAlerts = false;
- ExcelObj.Visible = false;
- xls.Workbook eBook = ExcelObj.Workbooks.Open(S_Directory + fileName, 2, false,
- 5, "", "", true, xls.XlPlatform.xlWindows, "",
- false, false, 0, false, true, 0);
- foreach (xls.WorkbookConnection wc in eBook.Connections)
- {
- if (wc.Type.ToString() == "xlConnectionTypeODBC")
- {
- wc.ODBCConnection.BackgroundQuery = false;
- }
- else
- {
- wc.OLEDBConnection.BackgroundQuery = false;
- }
- }
- eBook.RefreshAll();
- eBook.Save();
- NAR(eBook);
- ExcelObj.Workbooks.Close();
- ExcelObj.Quit();
- NAR(ExcelObj);
- }
- catch (Exception e)
- {
- ErroredFiles.Add(fileName.ToString());
- WriteErrorMessage(S_Directory, fileName.ToString(), e.Message.ToString());
- }
- }
- else if (fileName.ToString().Substring(1, 2) == "~$") { }
- }
- KillpIDs("Excel");
- if (ErroredFiles.Count > 0)
- {
- GenerateEmail("Data Refresh Failures");
- }
- }
- Moved byRoahn LuoMSFTFriday, November 06, 2009 9:18 AMoff-topic. (From:Visual C# General)
All Replies
- Hello,
If I may ask, are there any .xlsm files under the specified folder on the laptop? As far as I know, there are only two types of externsion for Microsoft Excel (.xls and .xlsx).
By the way, I'm going to move this thread to another forum since it is basically off-topic here. Hope you could understand.
Thanks,
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have any feedback, please tell us.
Welcome to the All-In-One Code Framework! - All the files are in fact Macro Enabled Files (XLSM).
Is there a better forum for this question? I'm going to post and cross reference in a different place, as soon as I determine, where it should go, but if you can move it to a more appropriate forum, I'd appreciate it.


