虽然实现了IDTExtensibility2接口和注册了Add-In,但是在Office中仍然可能出现Add-In未显示的情况。这可能是因为没有正确定义Add-In的加载行为或没有指定正确的Office应用程序版本。
以下是一个示例,其中Add-In在Word和Excel中都可见,并且可以通过'工具”菜单中的'COM Add-Ins”对话框进行管理。
using System;
using Extensibility;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
namespace MyAddIn
{
public class Connect : IDTExtensibility2
{
private object _applicationObject;
private object _addInInstance;
public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
{
_applicationObject = Application;
_addInInstance = AddInInst;
if (ConnectMode == ext_ConnectMode.ext_cm_Startup)
{
// Define the add-in's load behavior
var wordApp = _applicationObject as Word.Application;
if (wordApp != null)
{
var addIns = wordApp.AddIns;
var addIn = addIns.Add(Type: "MyAddIn", FileName: "C:\\MyAddIn.dll",
CopyFile: true, Install: true);
addIn.LoadBehavior = Word.wdLoadBehavior.wdLoadBehaviorLoadAtStartup;
}
var excelApp = _applicationObject as Excel.Application;
if (excelApp != null)
{
var addIns = excelApp.AddIns;
var addIn = addIns.Add(Type: "MyAddIn", FileName: "C:\\MyAddIn.dll",
CopyFile: true, Install: true);
addIn.AutoLoad = true;
}
}
}
public void OnDisconnection(ext_DisconnectMode DisconnectMode, ref Array custom)
{
// Clean up any objects, resources, etc.
}
public void OnAddInsUpdate(ref Array custom)
{
// Handle any changes to the list of add-ins
}
public void OnStartupComplete(ref Array custom)
{
// Perform any initialization required after Office has started up
}
public void OnBeginShutdown(ref Array custom)
{
// Perform any final tasks before shutting down
}
}
}
注意,此示例中的Add-In名称为'MyAddIn”,位于'C:\MyAddIn.dll”中。如果要在不同版本的