How can I prevent the deletion of columns in Excel with C# Excel add-in?

IINO,Takumi 20 Reputation points
2023-02-13T02:27:40.9433333+00:00

I have been developing an excel add-in in C#. The function I want to implement now is the ability to prevent the deletion of columns in an excel sheet. However, I have tried everything and for some reason it does not work.

I have tried 3 things.

  • The first thing I tried was to prevent the deletion of columns in the Protect function of Excel. This did indeed prevent the deletion of columns, but instead it also prevents the processing of some add-ins. For example, the addition of styles.
    1. The next method I tried was to protect the sheet with an add-in by adding UserInterfaceOnly=True. This did not protect it correctly. I am not sure I understand the behavior.
    1. The last thing I tried was to listen for a column delete event without protection and prevent it. That did not work either. I could not find a way to determine "column deletion" in the first place.

Thus, I could not find a way to prevent "only" column deletion. If anyone can help, it would be greatly appreciated.

Thank you in advance.

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Excel | For business | Windows
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Alan Farias 755 Reputation points
    2023-02-28T17:19:36.7833333+00:00

    One approach you could try is to handle the Worksheet.BeforeDelete event in your Excel add-in. This event is fired before any cells or columns are deleted, giving you a chance to cancel the deletion if it involves columns.

    Here's some sample code that demonstrates how to do this:

    // Assuming you have a reference to the current worksheet, set up the event handler like this:
    worksheet.BeforeDelete += Worksheet_BeforeDelete;
    
    // The event handler code:
    private void Worksheet_BeforeDelete(object sender, Excel.Range range, ref bool cancel)
    {
        // Check if any of the cells being deleted are column headers
        foreach (Excel.Range cell in range.Cells)
        {
            if (cell.EntireColumn.Row == 1) // Assuming row 1 contains column headers
            {
                // Cancel the delete operation
                cancel = true;
                return;
            }
        }
    }
    

    This code hooks up the Worksheet.BeforeDelete event and checks whether any of the cells being deleted are column headers (i.e. in row 1). If so, it cancels the delete operation by setting the cancel parameter to true. Note that this only prevents column deletion in the active worksheet. If you want to prevent column deletion in all worksheets in the workbook, you'll need to handle the event for each worksheet.

    You can modify this code to fit your specific scenario, for example, by checking for certain conditions before cancelling the deletion or displaying a message to the user.


    Please, if this answer is helpful, click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please let me know.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.