C# Program that uses excel throwing exception TYPE_E_CANTLOADLIBRARY

0

So I made a program that read some cells from an Excel file (.xlsx) and then do an online search with the data.

It is working fine on my computer (Windows 8.1, Visual Studio Community 2013, Office 2013) but when I send it to a friend on another machine (Windows 8.1, no Visual Studio, Office 2010) the program shows an error message saying (bad translation from portuguese):

System.InvalidCastException: It is not possible to convert the COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' on interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on component COM for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Error when loading the library/DLL of type. (Exceção de HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).

in

System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease) in Microsoft.Office.Interop.Excel.ApplicationClass.get_Workbooks() in ConsultaProcessos.MainForm.button1_Click(Object sender, EventArgs e)
in System.Windows.Forms.Control.OnClick(EventArgs e) in System.Windows.Forms.Button.OnClick(EventArgs e) in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) in System.Windows.Forms.Control.WndProc(Message& m) in System.Windows.Forms.ButtonBase.WndProc(Message& m) in System.Windows.Forms.Button.WndProc(Message& m) in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) em System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) in System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I asked him to instal VSTO 2010 and .NET Framework 4.5 but the error still happens.

My project has Excel 15.0 and Office 15.0 and VSTO 2010 added to the references.

Is there anything else that might be done for this to work? Thanks in advance!

see below for some of the code

using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;

xlApp = new Excel.Application();
Console.WriteLine(Directory.GetCurrentDirectory());
xlWorkBook = xlApp.Workbooks.Open(fileName);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

//These two lines do the magic.
xlWorkSheet.Columns.ClearFormats();
xlWorkSheet.Rows.ClearFormats();

iTotalColumns = xlWorkSheet.UsedRange.Columns.Count;
iTotalRows = xlWorkSheet.UsedRange.Rows.Count;

EDIT: I tried running in another computer with Office 2010 and it worked. It is not working on a particular PC with office 2010. I will see if the Registry fix suggested by Technovation will fix this.

c#
.net
excel
interop
vsto
asked on Stack Overflow May 5, 2015 by GuiFGDeo • edited May 6, 2015 by GuiFGDeo

1 Answer

0

my office package is 2013 and his is 2010

First of all, make sure that you use properties and methods that exists in the oldest Office version. You will get an exception trying to call non-existing method or property. I'd suggest embedding interop types into the assembly setting the Embed interop types property of the PIAs to true (available in .net 4.0 and later).

Does the target PC(problematic one) have the Click2Run edition of MS Office installed?

The fact is that the Click2Run edition of Office 2010 doesn't support automation. Click2Run apps have no automation interface and cannot be automated. See Office 2010 Click-to-Run compatibility with add-ins for more information. It states:

Out-of-process add-ins/applications are stand-alone programs, scripts, or applications that use Office object model APIs to start functionality in the application and integrate with Office. In this case, the out-of-process application drives Office. Out-of-process applications are not supported in Click-to-Run.

Also see the 0x80029C4A (TYPE_E_CANTLOADLIBRARY) similar forum thread.

answered on Stack Overflow May 6, 2015 by Eugene Astafiev

User contributions licensed under CC BY-SA 3.0