1

I am trying to solve an issue I am having with a COMException. This is my code:

The error occurs at Workbook Original = new Workbook(result[0]);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using MahApps.Metro;
using Microsoft.Win32;
using System.Windows.Forms;

using System.Data;
using Microsoft.Office.Interop.Excel;




namespace KPI_Generator_v3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : MetroWindow
{

    string [] result;
    Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();


    public MainWindow()
    {
        InitializeComponent();
    }

    private void exit_Click(object sender, RoutedEventArgs e)
    {
        this.Close();

    }

    private void browse_Click(object sender, RoutedEventArgs e)
    {

        // Create OpenFileDialog 
        instructionslbl.Visibility = Visibility.Hidden;
        dlg.Multiselect = true;
        dlg.ShowDialog();
        result = dlg.FileNames;
        dlg.DefaultExt = ".xls";
        dlg.Filter = "XLS Files (*.xls)|*.xls";

        foreach (string fileName in result)
        {
            displayFilesBox.Text += fileName + System.Environment.NewLine;

        }

        SelectedFileslbl.Visibility = Visibility.Visible;
        displayFilesBox.Visibility = Visibility.Visible;
        generateBtn.Visibility = Visibility.Visible;

    }

    private void generate_Click(object sender, RoutedEventArgs e)
    {
        Workbook Original = new Workbook(result[0]);
        for (int i = 1; i <= result.Length; i++)
        {

            Workbook next = new Workbook(result[i]);
            Worksheet newOGsheet = Original.Worksheets.Add();
            Worksheet nextSheet = next.Worksheets[0];
            nextSheet.Copy(newOGsheet);
        }



    }


    }
 }

Now.. Although the above code will probably not work I am getting an error that states

'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

with additional information saying

HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))

After googling for quite some time I have downloaded dependency walker and have the following output:

enter image description here

I have no idea what I am doing and some help would be much appreciated! Thank you

EDIT: REGEDIT picture of HKEY CLASSES_ROOT (if needed)

enter image description here

EDIT2: Picture of error

enter image description here

Paul
  • 521
  • 1
  • 5
  • 14
  • What line do you get the error on? From the code I can see one bug here `for (int i = 1; i <= result.Length; i++)` . I think it needs to be `for (int i = 1; i < result.Length; i++)` notice `<=` changed to `<`. You are exceeding the result array by 1. – Michael Petch Sep 22 '14 at 23:57
  • The error is on line 74 @ Workbook Original = new Workbook(result[0]); I believe my system does not have something registered properly. When I try to regsvr32 the Microsoft.Office.Interop.Excel.dll I get an error. Also, I am posting a picture of regedit at the HKEY CLASSES_ROOT value – Paul Sep 23 '14 at 00:05
  • By the way, these types of exceptions seem to commonly happen when you're using Office Interop. You might want to switch to something [Open Office XML SDK](http://msdn.microsoft.com/en-us/library/office/bb448854(v=office.15).aspx) or [EPPlus](http://epplus.codeplex.com/), as they are entirely in the .NET framework and don't rely on COM to communicate to external libraries. – mason Sep 23 '14 at 01:28
  • You cannot create WorkBook objects yourself, you have to use a factory function. Use Application.WorkBooks.Add() instead. Very common in the Office object model btw. There is a ton of example code out there to help avoid simple mistakes like this. – Hans Passant Sep 23 '14 at 01:58

1 Answers1

2

This error is a COM interop flaw unfortunately. Otherwise, you would be getting a compile error, since Workbook is an interface and doesn't have a constructor. This interface isn't meant to be instantiated, which is why you are getting this exception. (Not sure why you're passing in the filename here as well...)

To fix this, use Microsoft.Office.Interop.Excel.Application:

using Microsoft.Office.Interop.Excel;

Application excelApp = new Application();
excelApp.Workbooks.Open(result[0]);

Other notes:

  • You should call dlg.ShowDialog() last - that way, your filters will take effect.
  • For your filter, I suggest adding an option for .xlsx files, to support newer Excel files
  • Add result.Length > 0 before using it, just to make sure the user actually opened something.
  • Change your for loop condition to i < result.Length; otherwise, you'll get an IndexOutOfRangeException

Also, just to add more information: you may be asking: "but wait, ryrich, Application is an interface too! How come I can instantiate it but not Workbook??"

I wondered this too. Apparently this works because it is decorated with a CoClassAttribute (and some other attributes). For more detailed information about this, see this stack overflow post.

Community
  • 1
  • 1
ryrich
  • 2,164
  • 16
  • 24