none
Excel 2013: Single CustomTaskPane per window? RRS feed

  • Question

  • Hi,

    Since Excel 2013 is now SDI, I'm experiencing problems with my CustomTaskPanes (e.g, incorrect focus, "The taskpane has been deleted or is otherwise no longer valid" exceptions, etc).

    I'm now attempting to use the same methods for Word, which is also SDI (http://msdn.microsoft.com/en-us/library/bb264456.aspx).

    I want to create a single CustomTaskPane for each Excel 2013 window.

    If a CustomTaskPane has already been created for the window, then I want to re-use it.

    This code works in Word (2010, 2013), but not for Excel 2013.

    Somehow in Excel 2013, the CustomTaskPane.Window never matches the Application.ActiveWindow.

    Please help.

    Josh

    public void OnShowPane()
    {
        // Gets the active window (Application.ActiveWindow)
        var activeWindow = GetActiveWindow();
    
        // Find task pane for the current active window
        var pane = CustomTaskPanes.FirstOrDefault(ctp =>
            {
                var paneWindow = ctp.Window;
                var isPaneWindow = paneWindow == activeWindow;
                var isHostControl = ctp.Control is MyControlHost;
                return isPaneWindow && isHostControl;
            });
    
        // Create if not found
        if (pane == null)
        {
            pane = CustomTaskPanes.Add(new MyControlHost(), "My Pane", activeWindow);
        }
    
        // Make the pane, existing or newly-created, visible.
        if (!pane.Visible)
        {
            pane.Visible = true;
        }
    }


    [Update]

    The main problem is that each call to Application.ActiveWindow returns a new instance.

    Hence, (Application.ActiveWindow == Application.ActiveWindow) is ALWAYS false

    • Edited by Min Chew Wednesday, January 30, 2013 8:24 AM ActiveWindow never the same instance
    Wednesday, January 30, 2013 6:48 AM

Answers

  • Hi Min,

    Thanks for posting in the MSDN Forum.

    It's based on my experience that Microsoft.Office.Interop.Excel.Window infterface is a reference type value. You can use operator "==" to judge whether object on the left side of the "==" is the object on the right.

    I would recommend you compare the Hwnd property of the Window. That is a value type value.

    Following is my code which works fine on my side:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Office.Tools.Ribbon;
    using Office = Microsoft.Office.Tools;
    using Excel = Microsoft.Office.Interop.Excel;
    using System.Windows.Forms;
    
    namespace ExcelAddIn1
    {
        public partial class Ribbon1
        {
            private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, RibbonControlEventArgs e)
            {
                Excel.Application xlApp = Globals.ThisAddIn.Application;
                Office.CustomTaskPane ctp = Globals.ThisAddIn.CustomTaskPanes.FirstOrDefault();
                if (ctp == null)
                {
                    ctp = Globals.ThisAddIn.CustomTaskPanes.Add(new UserControl(), "test");
                    ctp.Visible = true;
                }
    
                Excel.Window a = xlApp.Workbooks.Add().NewWindow();
                var w = Globals.ThisAddIn.CustomTaskPanes.FirstOrDefault(WM =>
                {
                    Excel.Window paneWindow = WM.Window as Excel.Window;
                    var isPanWindow = false;
                    isPanWindow = paneWindow.Hwnd == a.Hwnd;
                    var isHostControl = ctp.Control is UserControl;
                    return isPanWindow && isHostControl;
                });
                
                if (w != null)
                {
                    MessageBox.Show(w.GetType().Assembly.FullName);
                    MessageBox.Show(((Excel.Window)((Office.CustomTaskPane)w).Window).Caption);
                }
                else
                {
                    MessageBox.Show("NO");
                }
            }
        }
    }

    Have a good day,

    Tom


    Tom Xu [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.

    Thursday, January 31, 2013 5:52 AM
    Moderator