正しくインストールされ、メイン アイコン (または空白のブック) から 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);
}
}
}
}