Excel任务窗格未显示

时间:2015-09-09 18:21:56

标签: c# vsto excel-addins customtaskpane

我正在尝试使用自定义任务窗格创建Excel加载项。我跟着this tutorial想要在用户单击功能区上的自定义按钮时显示Windows窗体控件。但是,我唯一可以让窗格显示的是我在This_AddIn_Startup事件上添加了一个测试消息框。这是相关的代码:

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)
    {

    }
}

我检查了this question并禁用了所有其他加载项,但仍然无效。

调试时如果我在btnPullData_Click上设置了一个断点,并注意到在没有显示时会抛出异常。以下是两个截图。

正确加载窗格(启用消息框)

enter image description here

窗格未加载(无消息框)

enter image description here

完整的例外文字:

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()}

我有点像C#的新手,我怎么去解决这个例外?我不明白添加MessageBox如何暂时解决问题。

编辑:更改了标题

1 个答案:

答案 0 :(得分:1)

我从未弄清楚为什么我的任务窗格从头开始创建时无法显示。但是,使用VSTO Contrib我能够将Excel Demo项目用作模板,并在需要时插入我自己的代码。自定义窗格每次都可以在Excel中轻松显示。

有用的视频开始here.

相关问题