开发者

How do I display a worksheet by name using VSTO 2010 for Excel

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

My need is simple, I 开发者_C百科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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜