tags:

views:

31

answers:

1

I have never used VSTO and I am finding it difficult to find a good learning aid for 2010.

My need is simple, I have a business workbook with 42 worksheets (I orignally guessed 20 but after counting found a surprising number). I want to add a ribbon (That part is easy) using VSTO to allow employees to navigate the large number of pages easily. I cannot seem to find the c# code to display a specific worksheet (Preferably by name) that I can simply add to the click event of the buttons.

Thanks

+1  A: 

Call the Activate method on the worksheet object (of type Microsoft.Office.Tools.Excel.Worksheet).

You can do this by name from within your ThisWorkbook class or via Globals.ThisWorkbook as follows:

private Excel.Worksheet GetWorksheetByName(string name)
{
  foreach (Excel.Worksheet worksheet in this.Worksheets)
  {
    if (worksheet.Name == name)
    {
      return worksheet;
    }
  }
  throw new ArgumentException();
}

private void ActivateWorksheetByName(string name)
{
  GetWorksheetByName(name).Activate();
}

Call the ActivateWorksheetByName and pass the name of the worksheet to show.

Richard Cook
How do I access the ThisWorkBook instance from within the Ribbon Class? I can't seem to get a hook on it and I obviously don't want to new up another one.
Mike B
You can access it via the `Globals` class as `Globals.ThisWorkbook`.
Richard Cook
Thanks Richard, I will try that as soon as I get into the office. I deperately need to find a good VSTO 2010 book. It seems that 99% of what I find on the web is outdated and I don't know enough to tell which 1% would be useful.
Mike B
The books by Eric Carter/Eric Lippert are good but I don't think there's a new edition for VSTO 2010 (version 4.0). However, most of the material for VSTO 2007 (version 3.0) is still relevant.
Richard Cook
Thanks, this did the trick. Oddly I get a compiler warning that the .Activate() is ambiguous between the method and event (It is using the method so it works fine) but it will not accept the full namespace. I appreciate your time, this solves my immediate issue and gives me a minor introduction into VSTO. Believe me I will buy the book when it is avaialbe.. I am not going to wait for the movie.
Mike B