locked
VSTO Excel Rename Tab or WorkSheet Tab Name... RRS feed

  • Question

  • I can't seem to rename the Excel tab using c# and VSTO. Nor can I find a clear answer to this, I am not sure how to assign the active worksheet tab to 'worksheet'...

    //declare the worksheet

    public Microsoft.Office.Tools.Excel.Worksheet workSheet;
    //trying to assign the active worksheet (sheet1);
    workSheet = this.WHAT GOES HERE?

    worksheet.Name= "New WorkSheet Name";

    I get a nullreference exception because I can't seem to assign the worksheet to the active worksheet...YES I'm totally new to Visual Studio and VSTO.

    Monday, July 1, 2013 2:21 AM

Answers

  • GOT IT!!!

     Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
               ws.Name="the new name of the Sheet";

    • Marked as answer by Quist Zhang Wednesday, July 3, 2013 6:35 AM
    Wednesday, July 3, 2013 6:28 AM

All replies

  • Hi Subvertio,

    Thank you for posting in the MSDN Forum.

    Could you please tell me what kind of project are you working on? Application-level Add-in or Document-level Customization?

    In C#, keyword "this" refers to the current instance of the class, thus We need to see more code give you further suggestions.

    I look forward to your reply.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 1, 2013 11:11 AM
  • This is an add in ... I'm not sure how to access the active worksheet, I have vsto 2012 installed and I am using excel 365 2013.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Windows.Forms;
    
    namespace EPF001
    {
        public partial class EPFRebalancer : UserControl
        {
    
           // I have no idea how to access the active worksheet! 
         public Microsoft.Office.Tools.Excel.Worksheet ws;
          
            public EPFRebalancer()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
           {
                (what is the parent that goes here)???Worksheet.Name="Work Sheet Name";
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("The 'Import Files' Button Below will Create 3 new worksheets in the current SpreadSheet");
            }
        }
    }

    Thanks for responding!!

    Wednesday, July 3, 2013 4:56 AM
  • GOT IT!!!

     Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveSheet;
               ws.Name="the new name of the Sheet";

    • Marked as answer by Quist Zhang Wednesday, July 3, 2013 6:35 AM
    Wednesday, July 3, 2013 6:28 AM