IIS 6.0COMDBAIISWebFormsVisual Studio 2005.NET 2.0SQL Server 2005C# 2.0中级开发Visual StudioSQL ServerSQLWindows.NETASP.NETC#
VSTO 2005 插件和 Outlook 2003
为 Outlook 2003 添加命令栏。
引言
VSTO 2005 允许您使用托管代码(C# 和 VB)为 Word 和 Excel 创建文档级别解决方案。文档级别解决方案与应用程序范围的 Word 或 Excel 插件略有不同。 VSTO 解决方案与文档相关联,并且解决方案的生命周期与文档的生命周期相同。 当您打开文档时,您的代码将被加载并运行。 当您关闭文档时,您的代码将被卸载。 Beta2 添加的一项新功能是为 Outlook 创建托管应用程序级插件的能力。
背景
IDTExensibility2
接口是 Office 插件背后的核心概念。
COM 插件是一个进程内 COM 服务器,或 ActiveX 动态链接库 (DLL),它实现了 Microsoft Add-in Designer 类型库 (Msaddndr.dll) 中描述的 IDTExensibility2
接口。 所有 COM 插件都从该接口继承,并且必须实现其五个方法中的每一个。
使用代码
代码包含两个主要部分
ThisAddIn_Startup
- ThisAddIn_Shutdown
在文档运行并且程序集中的所有初始化代码都已运行后,将引发 Startup。 这是在代码运行的类的构造函数中运行的最后一步。
Shutdown 为每个宿主项(文档或工作表)引发,当加载代码的应用程序域即将卸载时。 这是在类卸载时调用的最后一步。
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
try
{
Office.CommandBars cmdBars =
Application.ActiveExplorer().CommandBars;
Office.CommandBar cmdBar = cmdBars["Standard"];
try
{
myExtractButton = (Office.CommandBarButton)
cmdBar.Controls<btnextracttag>;
}
catch (Exception)
{
myExtractButton = null;
}
if (myExtractButton == null)
{
myExtractButton = (Office.CommandBarButton)
cmdBar.Controls.Add(1,missing ,missing ,
missing ,missing );
myExtractButton.Style =
Microsoft.Office.Core.MsoButtonStyle.msoButtonIconAndCaption;
myExtractButton.Picture =getImage();
myExtractButton.FaceId = 2521;
myExtractButton.Caption = btnExtractTag;
myExtractButton.Tag = btnExtractTag;
}
//event
myExtractButton.Click +=
new Microsoft.Office.Core.
_CommandBarButtonEvents_ClickEventHandler(myExtractButton_Click);
}
catch (Exception ex)
{
MessageBox.Show("Error initializing sample addin:\r\n" +
ex.Message, "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
void myExtractButton_Click(
Microsoft.Office.Core.CommandBarButton Ctrl,
ref bool CancelDefault)
{
Outlook.MAPIFolder folder = Application.Session.PickFolder();
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
将图标添加到命令按钮
包含以下类
class MyHost : System.Windows.Forms.AxHost
{
public MyHost(): base(null)
//("59EE46BA-677D-4d20-BF10-8D8067CB8B33")
{
}
public static stdole.IPictureDisp Convert(
System.Drawing.Image image)
{
return (stdole.IPictureDisp)
System.Windows.Forms.AxHost.GetIPictureDispFromPicture(image);
}
private stdole.IPictureDisp getImage()
{
stdole.IPictureDisp tempImage = null;
try
{
System.Drawing.Icon newIcon =Properties.Resources.Icon1 ;
System.Windows.Forms.ImageList imgList =
new System.Windows.Forms.ImageList();
imgList.Images.Add(newIcon);
tempImage = MyHost.Convert(imgList.Images[0]);
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
return tempImage;
}
}
注意:将您的图标文件放在资源文件夹中。