Error'A first chance exception of type 'System.Runtime.InteropServices.COMException'

Ftquesry

New member
Joined
Nov 12, 2017
Messages
1
Programming Experience
Beginner
I have this code to search for a text "FHD Camera" in column G in Sheet 2 in an excel file .I need to gets its address.

But i get and exception:
"A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in System.Dynamic.dll
Additional information: Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))
If there is a handler for this exception, the program may be safely continued."

I get this error for:
currentFind = range.Find("FHD Camera",
xlWorkSheet.Cells[1, 1],
Excel.XlFindLookIn.xlValues,
Excel.XlLookAt.xlPart,
Excel.XlSearchOrder.xlByRows,
Excel.XlSearchDirection.xlNext,
false,
false,
false);

Please help fix this.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;


namespace WindowsFormsApplication10
{
    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;
             Excel.Range currentFind = null;
             
 


            object misValue = System.Reflection.Missing.Value;


            xlApp = new Excel.Application();
 
             //~~> Change file name here
             xlWorkBook = xlApp.Workbooks.Open("C:\\Test.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
             xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);
 
             //~~> Set the Find Range as Col G
             Excel.Range range = (Excel.Range)xlWorkSheet.Columns["G", Type.Missing];
 
             //~~> Search for "xyz"
             currentFind = range.Find("FHD Camera",
              xlWorkSheet.Cells[1, 1],
              Excel.XlFindLookIn.xlValues,
              Excel.XlLookAt.xlPart,
              Excel.XlSearchOrder.xlByRows,
              Excel.XlSearchDirection.xlNext,
              false,
              false,
              false);
              string sAddress = currentFind.get_Address(false, false, Excel.XlReferenceStyle.xlA1, false, false);
 
             //~~> Display the found Address
             MessageBox.Show(sAddress).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();
             }
         } 
    }
}
 
Last edited by a moderator:
Does your app actually crash at that point? A first-chance exception message simply means that an exception was thrown. That's not necessarily a problem if the exception is caught and handled appropriately, which many are within the Framework. It's unhandled exceptions that are an issue, not first-chance exceptions.
 
Back
Top Bottom