Visual C# Developer Center >
Visual C# Forums
>
Visual C# General
>
C# and Microsoft.Office.Interop.Excel
C# and Microsoft.Office.Interop.Excel
- Hello,
Can someone please tell me what is incorrect about this code? I have a reference to Excel 12 included in the project...
using Excel = Microsoft.Office.Interop.Excel; namespace Program { class Program { static void Main(string[] args) { Excel.Application app = new Excel.Application(); app.DisplayAlerts = false; } } }
results in the following error:
System.InvalidCastException was unhandled Message="Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))." Source="Microsoft.Office.Interop.Excel" StackTrace: at Microsoft.Office.Interop.Excel.ApplicationClass.set_DisplayAlerts(Boolean RHS) at RefreshHelper.Program.Main(String[] args) in C:\Users\user\Desktop\program\program\Program.cs:line 20 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:
Thanks,
Dup- Edited byDuppy Geezus Tuesday, November 03, 2009 10:09 PM
- Edited byDuppy Geezus Tuesday, November 03, 2009 10:10 PM
All Replies
- First and foremost, remove your alias from your post.
Secondly, why do you have the extra NAMESPACE after Microsoft.Office.Interop.Excel?
What version of Excel do you have installed? Are you using Excel 2007 (Version 12)? - Recheck your references dialog.
Did you set a reference to the desired version of the Microsoft Excel Object Library from the COM tab of the Add Reference dialog? The resulting reference should appear as Microsoft.Office.Interop.Excel.
Hope this helps.
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Chris ...
Why remove the alias?
That is how I have always done it ... as shown here:
http://msmvps.com/blogs/deborahk/archive/2009/07/23/writing-data-from-a-datatable-to-excel.aspx
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS! - Hey,
Thanks for the heads up on my user alias, didn't want that included in the post!
I have version 12 (Excel 2007) installed... at one point I did have Office 2010 installed, but removed it and reinstalled Office 2007. The extra namespace was a typo in the post- Edited byDuppy Geezus Tuesday, November 03, 2009 10:16 PM
- Yes I have done this correctly. Even when I follow very simple examples, like the one above, the code will not run. Anything I try to do with Excel.Application throws an error. A few months ago I wrote an app similar to this and didn't have these problems... but that was on a different computer. The old computer that I didn't have trouble on was Vista. My current machine is Server 2008.
I have Office/Excel 12 installed on it.
Thanks! Chris ...
Why remove the alias?
That is how I have always done it ... as shown here:
http://msmvps.com/blogs/deborahk/archive/2009/07/23/writing-data-from-a-datatable-to-excel.aspx
www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
I wasn't sure, if he wanted his Alias published on a public forum. I know that I don't typically want my alias or anything that would link back to my employer published here.- As Deborah said, double check your reference. You may have had the Excel 14 lib referenced, so just remove that and reference the Excel 12 lib.
- Negative on referencing Excel 14... I am referencing Excel 12
Should I try reinstalling office or something? - Check in your component services under Microsoft Excel Application to see, if this ID 000208D5-0000-0000-C000-000000000046 is pointing to Excel 12 or 14.
- Thanks for this suggestion, but I'm not sure how to do this check, can you elaborate
- Start --> Admin Tools --> Component Services --> Console Root --> Component Services --> Computers --> My Computer --> DCOM Config --> Microsoft Excel Application
Right click and select properties
Check the application ID (should match the one in your error message) and the local path. If it's pointing to Excel 14, which you uninstalled, a repair of Office 12 may fix that.
This may or may not help. =P - Coincidentally I tried a repair before I got your steps, but following your steps I see the local path is listed as "C:\PROGRA~2\MICROS~3\Office12\EXCEL.EXE /automation"... so I guess it is pointing to Excel 12?
Repair didn't fix it.
This is really bumming me out... as far as I can tell my code is fine, but it just won't run - Also, some more info: I still have Visio and Project 2010 installed, and also "Microsoft Office 2010 Tools"... Don't know if it's relevant
- Well, I just got done talking to a coworker, who had both Office 2007 and 2010 installed, and he was having major issues with Excel, Powerpoint and Outlook. I have also experienced some issues with 2007 and 2010, so your best bet may be to completely remove 2010 and reinstall 2007. I have both 2007 and 2010 on my machine, and I do not have an issue running Excel from C#.
Well it is definitely something with the Office setup on this machine. I moved my C# project over to another 2008 server, installed Office 12, and the code ran exactly as it should.
hmmm- Well I just uninstalled and reinstalled Office 2007, but the script still will not run on this computer.
What else do I need to yank out?


