Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3662624
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:26:15+00:00 2026-05-19T01:26:15+00:00

VSTO 4.0 / Office 2007 In an Excel document-level automation project, I have a

  • 0

VSTO 4.0 / Office 2007

In an Excel document-level automation project, I have a worksheet that must not be deleted from the workbook. I am afraid that a careless user might delete it by accident, which currently causes a lot of grief (exceptions galore).

I can not protect the entire workbook, because the user must be able to create, delete and otherwise modify this file. Exactly one sheet needs to be protected from deletion, but I may be overlooking something, so if there exists such solution I’m all ears. For example I could imagine that I can Protect() and Unprotect() the workbook when the sheet is visible, but this solution seems messy.

Googling yielded the following VBA code:

Private Sub Worksheet_Activate()
Dim CB As CommandBar
Dim Ctrl As CommandBarControl
For Each CB In Application.CommandBars
Set Ctrl = CB.FindControl(ID:=847, recursive:=True)
If Not Ctrl Is Nothing Then
Ctrl.OnAction = "RefuseToDelete"
Ctrl.State = msoButtonUp
End If
Next
End Sub

I’m not familiar with VBA, but I tried running this from the VSTO generated startup method:

private void Sheet1_Startup(object sender, System.EventArgs e)
{
    //Is there a neater way to iterate through all Office Collections?
    for (var i = 1; i <= Application.CommandBars.Count; i++)
    {
        var commandBar = Application.CommandBars[i];
        //847 is a magical constant that any fule no has something to do with sheet deletion
        var control = commandBar.FindControl(Id: 847, Recursive: true);
        if (control != null) control.OnAction = null;
    }
}

This code seems to do exactly nothing. You may ask “Hey, Gleno, why are you setting OnAction to null” , well I don’t know what to set it to… The linked VBA solution attaches to Activate and Deactivate events, so there’s more code where that came from.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:26:16+00:00Added an answer on May 19, 2026 at 1:26 am

    I had to do something very similar today. I would just disable the sheet delete buttons whenever your one “undeleteable” sheet is active. If there’s a keyboard shortcut to delete a sheet, I can’t find one. (If there is, you could disable that too.)

    This would go in your ThisWorkbook class:

        private void ThisWorkbook_Startup(object sender, System.EventArgs e)
        {
            this.SheetActivate += (sh) =>
                {
                    this.ribbon.InvalidateBuiltinControl("SheetDelete");
                };
        }
    
        public bool CanDeleteActiveSheet()
        {
            if (this.ActiveSheet == null)
                return true;
    
            // Replace Sheet1 with your sheet's CodeName
            return ((Excel.Worksheet)this.ActiveSheet).CodeName != "Sheet1";
        }
    
        // Keep a local reference to the ribbon in your ThisWorkbook class
        // so you can call InvalidateControl() from it.
        Ribbon ribbon;
        protected override IRibbonExtensibility CreateRibbonExtensibilityObject()
        {
            this.ribbon = new Ribbon();
            return this.ribbon;
        }
    

    This would go in your ribbon code behind:

        public void InvalidateBuiltinControl(string controlID)
        {
            this.ribbon.InvalidateControlMso(controlID);
        }
    
        public bool deleteButton_GetEnabled(IRibbonControl control)
        {
            return Globals.ThisWorkbook.CanDeleteActiveSheet();
        }
    

    And this would go in your ribbon xml:

    <commands>
        <command idMso="SheetDelete" getEnabled="deleteButton_GetEnabled" />
    </commands>
    

    I’m still a little leery of holding on to that ribbon reference in ThisWorkbook, but so far no one has mentioned a better way in the question I posted earlier. Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code that worked in the VSTO version of an Excel Addin Microsoft.Office.Tools.Excel.ListObject
I have an Excel VSTO AddIn that I moved up to Excel 2007/VS2010 last
I have a VSTO Excel workbook that functions correctly in test. I can deploy
I have some C# automation code which does interesting things with Microsoft.Office.Interop.Word.Document objects. The
I have an excel 2007 Add-in built with VS2010 VSTO library. when i create
Hello I have the Addin for Word 2007 that is creted using VSTO. I
I have an Excel VSTO workbook project. The excel workbook uses windows service WCF
I have an VSTO excel 2007 Add in. For this I have created a
Are the VSTO add-ins that were developed for Office 2007 compatible with Office 2010?
I have a problem connected with Office 2007 and VSTO add-in. Description: Windows XP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.