【发布时间】:2012-12-18 15:39:04
【问题描述】:
我们制作了一个正确安装的 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);
}
}
}
}
【问题讨论】:
-
包含插件启动的代码可能会有用
-
ThisAddIn.cs 文件在“ThisAddIn_Startup”或“ThisAddIn_Shutdown”中没有任何内容,但我在上面的编辑中发布了它。
-
打开现有文档,转到文件 > 选项 > 加载项。查看您的加载项是否列为活动或非活动。
-
还可以尝试设置环境变量VSTO_SUPPRESSDISPLAYALERTS,看看在打开现有文档时是否收到任何错误消息。
-
它是选项插件菜单上的一个活动插件。我将上面的环境变量设置为“0”。打开文件时没有弹出消息框。