0

I can't access the Object Members of any event I create! - am I missing something important?

Current example:

I am developing an Excel Office Add-In. I have an ExcelFile class to manage the file but I am trying to put the event method in the Ribbon class so that I can change the ribbon controls.

In a nutshell, this piece of the program needs to:

  • Set my Custom Ribbon's Label Control text to the active sheet's name.

I have created a SheetActivate event for my C# Excel Office Add-In, using the following technique:

private event Excel.WorkbookEvents_SheetActivateEventHandler SheetActivateEvent;

...

public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
    this.ribbon = ribbonUI;

    ExcelFile TheFile = new ExcelFile();
    SheetActivateEvent = new Excel.WorkbookEvents_SheetActivateEventHandler(SheetActivate);
    TheFile.XLBook.SheetActivate += SheetActivateEvent;
}

...


private void SheetActivate(System.Object Sheet)
{
    //WorkSheetLabel = Sheet.Name; 
    this.ribbon.Invalidate(); 
}

...

public String GetSheetLabel(Office.IRibbonControl Control)
{
    return WorkSheetLabel;
}

The event works - if I place a breakpoint on the Ribbon Invalidate line I can clearly see the event is triggered every time I change the Active Sheet.

However I can't seem to access the Object's members. If I uncomment the Sheet.name code I receive errors, because there are no members in the Sheet object.

This problem occurs for my other events too.

How am I supposed to get the sheet name? Or do anything with the sheet, for that matter?

Hopefully I'm just being slow here...

Best Regards

Community
  • 1
  • 1
Robert
  • 531
  • 2
  • 7
  • 20

1 Answers1

0

as you see the Sheet is an Object. Before you can use you have to cast it to be the type of object it really is. In this case I susspect it's a Worksheet so:

private void SheetActivateEvent(System.Object Sheet)
{
    Worksheet castedSheet = Sheet as Worksheet;
    WorkSheetLabel = castedSheet.Name; //Should work now.
    this.ribbon.Invalidate(); 
}

UPDATE

try this:

 public void Ribbon_Load(Office.IRibbonUI ribbonUI)
{
    this.ribbon = ribbonUI;

    ExcelFile TheFile = new ExcelFile();
   // SheetActivateEvent = new Excel.WorkbookEvents_SheetActivateEventHandler(SheetActivate);
    TheFile.XLBook.SheetActivate += SheetActivate;
}
Adam Bilinski
  • 1,188
  • 1
  • 18
  • 28
  • On another note, I can't access my Ribbon using Globals.Ribbons. So therefore I can't change it from my ThisAddIn class. I think I am missing something fundamental here. I keep trying to read Articles and Tutorials but I can't seem to get it right. – Robert Jan 29 '14 at 11:39
  • Thanks for the update - however the result is still the same. I am trying to read more on this subject. I am clearly not understanding something very important here. – Robert Jan 29 '14 at 12:14