I'm trying to create an Excel Add-In with a custom task pane. I've followed this tutorial want to show a Windows Forms control when a user clicks a custom button on the ribbon. However, the only time I can get the pane to show is when I added a test message box on the This_AddIn_Startup
event. Here's the relevant code:
ThisAddIn.cs
private InputControl myInputControl { get; set; }
private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane {get;set;}
public CustomTaskPane TaskPane
{
get
{
return myCustomTaskPane;
}
}
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
MessageBox.Show("loading"); //If I comment this out, task pane won't show
myInputControl = new InputControl();
try
{
myCustomTaskPane = this.CustomTaskPanes.Add(myInputControl, "Test Task Pane");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
myCustomTaskPane.VisibleChanged +=
new EventHandler(myCustomTaskPane_VisibleChanged);
}
Ribbon.cs
public partial class Ribbon
{
private void Ribbon_Load(object sender, RibbonUIEventArgs e)
{
}
private void btnPullData_Click(object sender, RibbonControlEventArgs e)
{
Globals.ThisAddIn.TaskPane.Visible = true;
}
private void txtPosition_TextChanged(object sender, RibbonControlEventArgs e)
{
}
}
I checked out this question and disabled all other add-ins but that still didn't work.
While debugging if I put a break point on btnPullData_Click
and noticed there was an exception getting thrown when it was not showing. Here are the two screenshots.
Pane loads correctly (message box enabled)
Pane doesn't load (no message box)
Full exception text:
base = {System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Microsoft.Office.Core._CustomTaskPane.get_Window()
at Microsoft.Office.Tools.CustomTaskPaneImpl.get_Window()}
I'm somewhat of a newbie writing C#, how do I even go about troubleshooting that exception? I don't understand how adding in a MessageBox
temporarily fixes the problem.
Edit: Changed title