C# – System.Runtime.InteropServices.COMException

ccomexceptionexcelvba

I get the following error:

Unable to create instance of class TestProject.TestClass. Error: System.Runtime.InteropServices.COMException: 'D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS' could not be found. Check the spelling of the file name, and verify that the file location is correct. If you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.

Error Stack Trace:

Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad)
TestProject.TestLibrary.GetObjectDeclarations(String sModule) in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\TestLibrary.cs: line 133
TestProject.TestClass..ctor() in C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\TestProject\TestClass.cs: line 51

code:

using Excel = Microsoft.Office.Interop.Excel;

namespace TestProject
{
[TestClass]
public class TestLibrary
{
    public string[] arrObj = new string[19];
    public string[] arrConfig = new string[12];
    public string sobjfile;
    .
    .
    .
    xlApp = new Excel.Application();
        xlWorkBook = xlApp.Workbooks.Open(sobjfile, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

I am wondering why is the error saying D:\Automation\TestProject\OBJECT_DEFINITIONS.XLS could not be found when I have OBJECT_DEFINITIONS.XLS stored in C:\

Best Answer

And of course you need to assing path to public string sobjfile. Application doesn't know where to search your file.

EDIT:

using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Unable to release the Object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        } 
    }
}