Error Opening Excel 2007 File

-1

I am not very familiar with the OpenFileDialog class, but am using it to select a file and open the file in a C# winform app. This is my syntax, and when it hits the open line it has the correct filename, but it throws a debug error of System.Runtime.InteropServices.COMException: The server threw an exception RPC Info: Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)and if memory serves, that is one of the most generic messages you can get. Syntactically is this set-up properly? Or how can I achieve my .xlsx file to be opened?

OpenFileDialog fd = new OpenFileDialog();
        fd.Title = "Select The File To Open";
        fd.Filter = "Excel Files|*.xlsx";
        fd.InitialDirectory = @"C:\";
        if (fd.ShowDialog() == DialogResult.OK)
        {
            Excel.Application xlsApp = new Excel.Application();
            try
            {
                Workbook wb = xlsApp.Workbooks.Open(fd.FileName);
                MessageBox.Show("The file was opened");
            }
            catch (Exception grrr) { MessageBox.Show(grrr.ToString()); }
        }
c#
openfiledialog
import-from-excel
comexception
asked on Stack Overflow Mar 25, 2015 by user2676140 • edited Mar 25, 2015 by pnuts

1 Answer

1

Check your references. For example, if you have Excel 2000 set as default on the computer you are running this project on, but the reference is set for Excel 2007 (would show as Excel 12.0 object library) you will receive the above HRESULT error.

Quickest way to check IMO, is to look in your Solution Explorer and click the drop down arrow beside References and select the Microsoft.Office.Interop.Excel and see what version is listed in the Description.


User contributions licensed under CC BY-SA 3.0