0
votes

I have the below code which works to delete the first row of a specified excel workbook. After this is done I would like to save (Overwrite changes) and exit the excel applications.

I gathered this may be achieved by Workbook.Close(True) but the popups still occur and the Object workbook is not referenced.

Any help would be much appreciated.

public void DeleteRows(string workbookPath)
    {

        // New Excel Application 
        Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
        //Open WorkBook 
        Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
                0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
                true, false, 0, true, false, false);

        Microsoft.Office.Interop.Excel.Sheets excelSheets = excelWorkbook.Worksheets;
        string currentSheet = "Sheet1";
        Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)excelSheets.get_Item(currentSheet);

        Microsoft.Office.Interop.Excel.Range excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range("A1", "A1");
        Microsoft.Office.Interop.Excel.Range row = excelCell.EntireRow;
        row.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlUp);


    }
1
In your case "Workbook" is excelWorkbook ...which is most certainly referencedBugFinder
Where do you close the workbook? Also it is good practice to correctly dispose of all interop COM objects - using Marshal.ReleaseComObject. I always aim to go in reverse order of creation - so row, excelCell, excelWorksheet, etcPaulF

1 Answers

0
votes

Honestly, all I think that's missing from your code is a save and close. Once you save, the warning should not be issued.

using Excelx = Microsoft.Office.Interop.Excel;

public void DeleteRows(string workbookPath)
{

    // New Excel Application 
    Excelx.Application excelApp = new Excelx.Application();
    //Open WorkBook 
    Excelx.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath,
            0, false, 5, "", "", false, Excelx.XlPlatform.xlWindows, "",
            true, false, 0, true, false, false);

    Excelx.Sheets excelSheets = excelWorkbook.Worksheets;
    string currentSheet = "Sheet1";
    Excelx.Worksheet excelWorksheet = (Excelx.Worksheet)excelSheets.get_Item(currentSheet);

    Excelx.Range excelCell = (Excelx.Range)excelWorksheet.get_Range("A1", "A1");
    Excelx.Range row = excelCell.EntireRow;
    row.Delete(Excelx.XlDirection.xlUp);

    // This should be all you need:

    excelWorkbook.Save();
    excelWorkbook.Close();
}

I'd be puzzled if this doesn't work, but if it doesn't, it might help when you are debugging to make Excel visible and step through the code:

excelApp.Visible = true;

As a final last attempt, before you save, you can disable warnings which will tell Excel to dispense with any of the dialogs to try to save you from yourself:

excelApp.DisplayAlerts = false;

excelWorkbook.Save();
excelWorkbook.Close();

excelApp.DisplayAlerts = true;

And again, I think you shouldn't even get to step 2 for this to work, but let me know if it doesn't. We would have quite a puzzle.