c# - 我的 Excel 2010 加载项仅在打开空白工作簿时显示。打开现有文档时不会显示

标签 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
}

这是我们制作的 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);
      }
    }
  }
}

最佳答案

抱歉回答晚了,但这是一个很常见的问题,所以我还是会回答的。我自己遇到过几次,和我的addin的代码没有关系。问题出在资源管理器的预览 Pane 中。 enter image description here当您在资源管理器中选择 Excel 文件时,它会启动一个 Excel 实例进行预览。然后你在真正的 Excel 中打开你的文件,Excel 中的一些错误阻止所有加载项加载。您的插件甚至不会启动任何代码,因此您无法从插件内部执行任何操作。唯一的方法是不对 excel 文件使用预览。更糟糕的是,预览一个文件后,Excel 进程仍然卡在内存中,因此插件将无法工作,直到您从任务管理器中将其终止。此错误存在于 Excel 2007、2010、2013 甚至 2016 中。

关于c# - 我的 Excel 2010 加载项仅在打开空白工作簿时显示。打开现有文档时不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13936388/

相关文章:

excel - 为什么 Excel 2010 VBA 用户窗体文本框字体会根据框架大小而变化?

excel - For循环删除所有行

excel - 在 Excel VBA 中设置全局变量

c# - VSTO - C# 中的 Outlook 事件处理程序

c# - 如何在 .NET Core 3 的 Controller 构造函数中访问 HttpContext?

c# - 在 webbrowser c# 控件中停止警报 javascript 弹出窗口

c# - 从捕获中排除异常类型的最佳方法是什么?

c# - 在 Roslyn 的 .net core 中动态选择引用

c# - ftp上传太大

c# - 添加 WPF 命名空间导致 VSTO 编译错误