我的Excel 2010加入仅在打开空白工作簿时显示.打开现有文档时不会显示

bla*_*ley 6 c# excel vsto add-in

我们已经制作了一个正确安装的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
}
Run Code Online (Sandbox Code Playgroud)

这是我们制作的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);
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*nko 1

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