Question

I have a CommandBarPopup on a the context menu in excel that contains three CommandBarButtons, one of those buttons opens a web page, the other two open a custom task pane.

If I make the custom task pane containing an element host which hosts a WPF user control visible then the any of the CommandBarButtons I have added will stop working. Even if I close the custom task pane it will still not work.

If I do the same with a custom task pane container a web browser it seems to work fine.

Here is the code we are using

private void InitializeComponent()
    {
        this.elementHost1 = new System.Windows.Forms.Integration.ElementHost();
        this.myView = new MyView();
        this.SuspendLayout();
        // 
        // elementHost1
        // 
        this.elementHost1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.elementHost1.Location = new System.Drawing.Point(0, 0);
        this.elementHost1.Name = "elementHost1";
        this.elementHost1.Size = new System.Drawing.Size(780, 560);
        this.elementHost1.TabIndex = 0;
        this.elementHost1.Text = "elementHost1";
        this.elementHost1.Child = this.myView;
        // 
        // MyTaskPane
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.elementHost1);
        this.Name = "MyTaskPane";
        this.Size = new System.Drawing.Size(780, 560);
        this.ResumeLayout(false);

    }
Was it helpful?

Solution

So the answer was that the CommandBarButtons were being disposed once the variable scope ended which was surprising as I would have assumed they would be attached to the excel application object. Also looking at the excel commandbar, I could see the buttons there but clicking on them resulted in the click event not triggering. Anyway I stored them in a class variable and it worked again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top