none
VSTO Excel 2010 Document AddIn (not VBA) - Worksheet X got focus event? RRS feed

  • Question

  • Hi,

    I have an Excel 2010 Document AddIn (VS2010, C#, .Net 4.0, VSTO) and I am trying to find an event for when a worksheet gets focus (I use the word "focus" intentionally here because Excel.WorkbookEvents_SheetActivateEventHandler fires for every worksheet when the user clicks just the one worksheet tab).  What I am after is when the end-user clicks one of the worksheet tabs (to make it the worksheet he/she will work on), I would like to code the event handler within the worksheet class that will allow me to make updates to one of the controls within a Ribbon group specifically for that worksheet (i.e. the one that the end-user clicked).  As an example, if there is a label control in a Ribbon Group, I would make the label text be the worksheet name that is currently visible to the end-user.  Is there such an event to let me do this?  I looked at the list of events and tried SheetActiveEventHandler (which looked like it was what I wanted) but this seems to fire on each sheet (and not even in an order where I could derive the actual active worksheet).  Any advice/help here would be greatly appreciated.

    Thanks in advance.


    Anthony LaMark
    Sunday, September 11, 2011 2:45 AM

Answers

  • Hi Anthony,

     

    Thanks for you posting in the MSDN Forum.

     

    According to you description, I create this snippet to fit your goal, let’s see whether this snippet can solve you issue.

     

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml.Linq;
    using Microsoft.Office.Tools.Excel;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    
    namespace ExcelWorkbook1
    {
        public partial class ThisWorkbook
        {
            private void ThisWorkbook_Startup(object sender, System.EventArgs e)
            {
                Excel.Application exApplication = Globals.ThisWorkbook.Application;
                exApplication.SheetActivate += new Excel.AppEvents_SheetActivateEventHandler(exApplication_SheetActivate);
            }
    
            void exApplication_SheetActivate(object Sh)
            {
                if (Sh is Excel.Worksheet)
                {
                    Excel.Worksheet ws = (Excel.Worksheet)Sh;
                    if(ws.Name.Equals("Sheet3"))
                        MessageBox.Show("Just fire");
                }
            }
    
            private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisWorkbook_Startup);
                this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
            }
    
            #endregion
    
        }
    }
    
    

     

    Have a good day,

     

    Tom

     

     

     


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ALaMark Monday, September 12, 2011 3:40 PM
    Monday, September 12, 2011 7:14 AM
    Moderator

All replies

  • Hi Anthony,

     

    Thanks for you posting in the MSDN Forum.

     

    According to you description, I create this snippet to fit your goal, let’s see whether this snippet can solve you issue.

     

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml.Linq;
    using Microsoft.Office.Tools.Excel;
    using Microsoft.VisualStudio.Tools.Applications.Runtime;
    using Excel = Microsoft.Office.Interop.Excel;
    using Office = Microsoft.Office.Core;
    
    namespace ExcelWorkbook1
    {
        public partial class ThisWorkbook
        {
            private void ThisWorkbook_Startup(object sender, System.EventArgs e)
            {
                Excel.Application exApplication = Globals.ThisWorkbook.Application;
                exApplication.SheetActivate += new Excel.AppEvents_SheetActivateEventHandler(exApplication_SheetActivate);
            }
    
            void exApplication_SheetActivate(object Sh)
            {
                if (Sh is Excel.Worksheet)
                {
                    Excel.Worksheet ws = (Excel.Worksheet)Sh;
                    if(ws.Name.Equals("Sheet3"))
                        MessageBox.Show("Just fire");
                }
            }
    
            private void ThisWorkbook_Shutdown(object sender, System.EventArgs e)
            {
            }
    
            #region VSTO Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InternalStartup()
            {
                this.Startup += new System.EventHandler(ThisWorkbook_Startup);
                this.Shutdown += new System.EventHandler(ThisWorkbook_Shutdown);
            }
    
            #endregion
    
        }
    }
    
    

     

    Have a good day,

     

    Tom

     

     

     


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ALaMark Monday, September 12, 2011 3:40 PM
    Monday, September 12, 2011 7:14 AM
    Moderator
  • Hi Tom,

    EXCELLENT!  That was very clever to check the name inside the event handler!  Thank you very much for your help!  Take care.

    Tony


    Anthony LaMark
    Monday, September 12, 2011 3:42 PM