6

我们制作了一个正确安装的 excel 插件,仅在从主图标(或空白工作簿)打开 Excel 时才会显示。打开任何现有保存的 Excel 文档时,它不会显示在工具栏上。

我已确保在打开现有文档时,在文件 -> 选项 -> 插件下,它已在 COM 插件中正确检查。为了使用我们的插件,我们必须打开一个空白工作簿,并将我们现有的文件拖到空白工作簿中。

有谁知道为什么它只会出现在空白工作簿的功能区中而不是现有的 .xlsx 文件中?

我什至运行了一个测试,我打开一个空白工作簿,确认加载项在功能区上,在单元格中放入一些文本,将其保存到我的桌面,关闭它,然后重新打开它。然后它不会出现。这个插件是用 VS2010 制作的。

这是来自“ThisAddIn.cs”的代码

public partial class ThisAddIn
{
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
    }

    #region VSTO generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InternalStartup()
    {
        this.Startup += new System.EventHandler(ThisAddIn_Startup);
        this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
    }

    #endregion
}

这是我们制作的 Ribbon.cs 文件中的代码......它所做的只是填充一些字段并进行设置:

private void MyRibbon_Load(object sender, RibbonUIEventArgs e)
{

  Excel._Workbook activeWorkbook = (Excel._Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
  if (activeWorkbook.Path == "")
  {
    string pathMyDocuments = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
    this.editBox1.Text = pathMyDocuments;
  }
  else
  {
    this.editBox1.Text = activeWorkbook.Path;
    this.fileBox.Text = "Converted_" + activeWorkbook.Name;
  }

  this.folderBrowserDialog1.RootFolder = System.Environment.SpecialFolder.MyComputer;
  this.folderBrowserDialog1.ShowNewFolderButton = true;

  //populate the dropdown box with spreadsheet templates
  using (SqlConnection conn = new SqlConnection("<removed for stack overflow>"))
  {
    using (SqlCommand command = new SqlCommand("<sql command text removed for SO", conn))
    {
      command.CommandType = CommandType.Text;

      conn.Open();
      SqlDataReader reader = command.ExecuteReader();

      while (reader.Read())
      {
        RibbonDropDownItem item = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem();
        item.Label = reader["MasterSpreadsheetName"].ToString();
        ddlSpreadsheetTemplate.Items.Add(item);
      }
    }
  }
}
4

1 回答 1

1

抱歉回答晚了,但这是很常见的问题,所以无论如何我都会回答。我自己也遇到过几次,和我插件的代码无关。问题出在资源管理器的预览窗格中。在此处输入图像描述当您在资源管理器中选择您的 Excel 文件时,它会启动一个 Excel 实例以供预览。然后你在真正的 Excel 中打开你的文件,Excel 中的一些错误会阻止所有插件加载。您的插件甚至不会启动任何代码,因此您无法从插件内部执行任何操作。唯一的方法是不要对 excel 文件使用预览。更糟糕的是,在预览一个文件后,Excel 进程仍然挂在内存中,因此插件将无法工作,直到您从任务管理器中将其杀死。此错误存在于 Excel 2007、2010、2013 甚至 2016 中。

于 2016-01-04T10:12:27.530 回答