Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel VSTO Addin showing/hiding taskpane

Tags:

c#

excel

vsto

I do the tutorial here. Everything works fine with a blank excel page

https://msdn.microsoft.com/en-us/library/bb608590(v=vs.120).aspx

When I load up a excel sheet someone gave me and go to click the toggleButton1 to show the pane I get

{"The taskpane has been deleted or is otherwise no longer valid."}

on the line

   private void toggleButton1_Click(object sender, RibbonControlEventArgs e)
    {
        Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
    }

Is a pointer to that task pane somehow going away?

Microsoft.Office.Tools.CustomTaskPane PartPhotoTaskPane;
Globals.ThisAddIn.Application.WindowActivate += Application_WindowActivate;

        void Application_WindowActivate(Excel.Workbook Wb, Excel.Window Wn)
    {
        if (PartPhotoTaskPane != null)
        {
            PartPhotoTaskPane.Dispose();
            InitalizePartPhotoViewerTaskPane(EPPF);
        }
        else
        {
            InitalizePartPhotoViewerTaskPane(EPPF);
        }
    }

    /// <summary>
    /// Start up the part photo viewer task pane
    /// </summary>
    private void InitalizePartPhotoViewerTaskPane(ExcelPartPhotoFunctions _EPPF)
    {
        //intialize the part search
        try
        {
            PartPhotoTaskPane = Globals.ThisAddIn.CustomTaskPanes.Add(new PartPhotoSearchPane(_EPPF), "Part Information", Globals.ThisAddIn.Application.ActiveWindow);
            PartPhotoTaskPane.Visible = Properties.Settings.Default.InfoPaneOpenStatus;
            PartPhotoTaskPane.Width = 260;
        }
        catch (Exception e)
        {
            MessageBox.Show("Error starting Part Info Toolbar:" + Environment.NewLine +
            e.Message + Environment.NewLine + e.StackTrace, "Error!", MessageBoxButtons.OK,
            MessageBoxIcon.Error);
        }
    }

ATTEMPT 1:

Still error on the click event. I am guessing it is something related to me needing to share that pane between classes?

private void toggleButton1_Click(object sender, RibbonControlEventArgs e)
    {
        Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
    }


  private void ExcelEvents_WorkbookActivate(Excel.Workbook wb)
    {
        var taskPane = (customTaskPanes.Where(kvp => kvp.Key.Target == wb).Select(kvp => kvp.Value).FirstOrDefault());

        if (taskPane == null)
        {
            UcCenlarInvest control = new UcCenlarInvest();
            taskPane = this.CustomTaskPanes.Add(control, "My pane for workbook " + wb.Name);
            customTaskPanes[new WeakReference(wb)] = taskPane;
        }
    }

ATTEMPT 2:

So now the Ribbon class can get the TaskPane yet I Still get the same error. Added this :

  private CustomTaskPane taskPane;
    public CustomTaskPane TaskPane
    {
        get
        {
            //return (customTaskPanes.Where(kvp => kvp.Key.Target == wb).Select(kvp => kvp.Value).FirstOrDefault());
              return pane;
        }
        set
        {
            taskPane = value;
        }
    }

.....

  TaskPane = (customTaskPanes.Where(kvp => kvp.Key.Target == wb).Select(kvp => kvp.Value).FirstOrDefault());
like image 404
punkouter Avatar asked Dec 01 '15 18:12

punkouter


1 Answers

Excel 2016 is a single document interface (SDI), each workbook in a single instance of Excel contains its own ribbon UI. more information

If you open a new workbook, a new ribbon appears, however the pointer to the taskpane is lost. You should implement a WorkbookActivate event and add a new task pane if no task pane already exist for it. You should also keep a static list of pointer to the custom taskpanes.

see this solution : CustomTaskPane in Excel doesn't appear in new Workbooks

like image 59
Malick Avatar answered Oct 15 '22 04:10

Malick