Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to delete a row from excel, but I am getting like this "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))". The code is given bellow
C#
try
            {

                ExcelApplication = new Microsoft.Office.Interop.Excel.Application();
                ExcelWorkbook = ExcelApplication.Workbooks.Open(ExcelFileName, Missing.Value, Missing.Value, Missing.Value
                                                          , Missing.Value, Missing.Value, Missing.Value, Missing.Value
                                                         , Missing.Value, Missing.Value, Missing.Value, Missing.Value
                                                        , Missing.Value, Missing.Value, Missing.Value);
                ExcelWorksheet = (Worksheet)ExcelWorkbook.Worksheets.get_Item(ExcelSheetName);
                ExcelWorkSheetRange = (Range)ExcelWorksheet.Cells.Find("SNO22", "SNO22", XlFindLookIn.xlValues, XlLookAt.xlWhole, XlSearchOrder.xlByRows, XlSearchDirection.xlNext,Missing.Value, false, false);
                ExcelWorkSheetRange = ExcelWorksheet.get_Range(CellRange1, CellRange2);
                ExcelWorkSheetRange = ExcelWorkSheetRange.EntireRow;
                ExcelWorkSheetRange.Delete(Missing.Value);
                ExcelWorkbook.Save();
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
            finally
            {
                ExcelWorkbook.Close(false, Missing.Value, Missing.Value);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkbook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorksheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApplication);
            }

if some one knows my Mistake, Please Help me to put out
Thankx
Posted
Updated 9-Jul-12 4:03am
v2
Comments
ZurdoDev 9-Jul-12 10:07am    
Which line of code causes the error?
K C Behera 11-Jul-12 1:42am    
ExcelWorkSheetRange = (Range)ExcelWorksheet.Cells.Find("SNO22", "SNO22", XlFindLookIn.xlValues, XlLookAt.xlWhole, XlSearchOrder.xlByRows, XlSearchDirection.xlNext,Missing.Value, false, false);
Jim Jos 9-Jul-12 10:21am    
You have used ExcelWorksheetRange variable for find then get_range .. I think the problem is somewhere there.. After finding why do get_range?
K C Behera 11-Jul-12 1:42am    
I want to find the row using search value, after getting that will delete
Jim Jos 11-Jul-12 2:59am    
Ya that's wat I am saying -- Find itself will return a range you could delete that range itself.. get_range unneccessarily complicates the issue.. Why don't you remove the get_range and try?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900