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.