-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()); }
        }
pnuts
  • 58,317
  • 11
  • 87
  • 139
Bob Goblin
  • 1,251
  • 3
  • 16
  • 33
  • I believe `COMException` normally gives an HRESULT error code that can help you decipher what has gone wrong. – Phylogenesis Mar 25 '15 at 01:19
  • Do you really *need* to interact with Excel? If you're just trying to import data from an Excel file, there's better ways to do that. Use a native library without external dependencies such as Open XML SDK, EPPlus, or NPOI (EPPlus is my choice). The're lighter weight, faster, have no dependencies, and at least the last two are easier to work with. – mason Mar 25 '15 at 12:42
  • @mason - all I am needing to do is open the workbook, select the 1st column, ignore the 1st row, then read the remainder of the 1st column into an array. Is this feasible using one of the native library's mentioned above? – Bob Goblin Mar 25 '15 at 12:55
  • @user2676140 Yes, of course. – mason Mar 25 '15 at 13:14
  • @mason - interesting. I will spend some time on google, looking up how to achieve such with EPPlus – Bob Goblin Mar 25 '15 at 13:18

1 Answers1

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.