Problem in deleting row using excel interop object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shahgunjan
    New Member
    • Mar 2010
    • 1

    Problem in deleting row using excel interop object

    Hi,

    I am reading the value of a cell and struggling to delete that entire row and shift other rows up.

    I googled a lot but did not find any solution.
    The code that I am using is :

    // create the workbook object by opening the excel file.
    Workbook workBook = app.Workbooks.O pen(Path, 0, true, 5, "", "", true, XlPlatform.xlWi ndows,
    "\t", false, false, 0, true, 1, 0);

    // Get The Active Worksheet Using Sheet Name Or Active Sheet
    Worksheet workSheet = (Worksheet)work Book.ActiveShee t;

    // Select a cell in the excel
    object rowIndex = 2;
    object colIndex = 3;
    lblemailno.Text = "Please wait while E-mails are sent...";

    while (((Range)workSh eet.Cells[rowIndex, colIndex]).Value2 != null)
    {
    string emailto = ((Range)workShe et.Cells[rowIndex, colIndex]).Value2.ToStri ng();
    workSheet.get_R ange(rowIndex, colIndex).Entir eRow.Delete(XlD eleteShiftDirec tion.xlShiftUp) ;

    SendEmail(email to);
    rowIndex = ++gindex;
    progressBar1.Ma ximum = 8;
    progressBar1.Va lue = 0;
    InitializeMyTim er();
    }
    lblemailno.Text = "E-mails sent Successfully";
    btnSendMail.Ena bled = true;
    }

    When I run this code I get HRESULT exception at line:
    workSheet.get_R ange(rowIndex, colIndex).Entir eRow.Delete(XlD eleteShiftDirec tion.xlShiftUp) ;

    Looking forward for any help..
Working...