6

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());
punkouter
  • 5,170
  • 15
  • 71
  • 116
  • what version of Excel do you have ? – Malick Dec 01 '15 at 18:58
  • This is 2016. Though a person who is on 2013 uses it and has same issue. I have a awkward workaround of pasting the data into the working blank excel sheet but that is not good... any there is very little help on this topic (C# Excel Application plguins) on the internet – punkouter Dec 01 '15 at 19:03

2 Answers2

6

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

Community
  • 1
  • 1
Malick
  • 6,252
  • 2
  • 46
  • 59
  • So in theory this error should not happen in Excel 2013? (I will install that today to try it) – punkouter Dec 02 '15 at 13:27
  • @punkouter no you will have the same problem with Excel 2013 since it is also a SDI - see the link "more information". – Malick Dec 02 '15 at 14:28
  • I think my problem is related to the line Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked; when I refer to that TaskPane I need to refer to the code that looks for a pane and creates a new one if it does not exists.. But that is in another event – punkouter Dec 02 '15 at 16:40
  • I fixed it.. I had 2 Custom panes in the code by mistake and that was confusing things – punkouter Dec 02 '15 at 17:19
2

You can also work around this by checking the taskpane control's IsDisposed property, for example

if (taskPane.CustomTaskPane?.Control.IsDisposed == false)
{
    taskPane.CustomTaskPane.Visible = false;
}
GCymbala
  • 343
  • 1
  • 3
  • 10