none
How to set focus to actve worksheet from backstage click handler? RRS feed

  • Question

  • I have a backstage button which, when clicked, creates a new worksheet and places some content in the sheet. I want this backstage click handler to switch focus to the worksheet after the content is added to the sheet. Is this possible?

    thanks,

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
    
      <backstage>
        <button id="butBackstage" label="ExcelAddin2" onAction="butBackstage_OnAction"  
                imageMso="HappyFace" />
      </backstage>
    
    </customUI>
    
    

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Text;
    using Office = Microsoft.Office.Core;
    using InteropExcel = Microsoft.Office.Interop.Excel;
    
    namespace ExcelAddIn2
    {
      [ComVisible(true)]
      public class Ribbon1 : Office.IRibbonExtensibility
      {
        private Office.IRibbonUI ribbon;
    
        public Ribbon1()
        {
        }
    
        public void butBackstage_OnAction(Office.IRibbonControl control)
        {
          var wb = Globals.ThisAddIn.Application.ActiveWorkbook as InteropExcel.Workbook;
          var sheets = wb.Sheets;
          var ws = (InteropExcel.Worksheet) sheets.Add();
          ws.Cells[1, 1].Value = "abc";
    
          // how to switch the focus to the newly added worksheet ???
        }
    
        #region IRibbonExtensibility Members
    
        public string GetCustomUI(string ribbonID)
        {
          return GetResourceText("ExcelAddIn2.Ribbon1.xml");
        }
    
        #endregion
    
        #region Ribbon Callbacks
        //Create callback methods here. For more information about adding callback methods, visit http://go.microsoft.com/fwlink/?LinkID=271226
    
        public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
          this.ribbon = ribbonUI;
        }
    
        #endregion
    
        #region Helpers
    
        private static string GetResourceText(string resourceName)
        {
          Assembly asm = Assembly.GetExecutingAssembly();
          string[] resourceNames = asm.GetManifestResourceNames();
          for (int i = 0; i < resourceNames.Length; ++i)
          {
            if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0)
            {
              using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i])))
              {
                if (resourceReader != null)
                {
                  return resourceReader.ReadToEnd();
                }
              }
            }
          }
          return null;
        }
    
        #endregion
      }
    }
    

    Friday, May 15, 2015 2:27 AM

All replies

  • the built in backstage buttons for New and Open automatically switch to the active spreadsheet when their execution is complete. Why can't backstage code of my own switch the focus to the active sheet?

    Sunday, May 17, 2015 10:31 PM
  • Hi,

    I have followed the code to develop a backstage button to add a new workbook, each time I add a new worksheet, the focus will be to the new added sheet. Since I can't reproduce this issue, could you please provide more information about the environment? Do you have any other add-in in your Excel, Can you disable them and do it agagin? And what's the version of your Excel, have you update your Office ?

    Best Regards,

    Lan


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 19, 2015 8:27 AM
    Moderator