0

我正在尝试使用自定义任务窗格创建 Excel 加载项。我已经按照本教程希望在用户单击功能区上的自定义按钮时显示 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);

    }

功能区.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)
    {

    }
}

我检查了这个问题并禁用了所有其他加载项,但这仍然不起作用。

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

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

在此处输入图像描述

窗格未加载(无消息框)

在此处输入图像描述

完整的异常文本:

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临时解决问题。

编辑:更改标题

4

1 回答 1

1

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

从这里开始的有用视频

于 2015-11-04T14:45:09.233 回答