设为首页 收藏本站
查看: 588|回复: 0

[经验分享] Hello World Outlook Add-In using C#

[复制链接]

尚未签到

发表于 2015-9-13 10:18:44 | 显示全部楼层 |阅读模式
One thing I’d like to play with is extending Outlook through add-ins with C#. It’ll be a good opportunity to learn more about .NET development and the Windows tools. Plus, I can “fix” some of the things that annoy me about Outlook. I’ve been talking to Omar about this too, and hopefully we can collaborate on a few projects. We’ve been sharing a few links back and forth on getting started. It’s a bit hard to find the right information to get started, but it’s out there. I’ve compiled steps below for a “hello world” type Outlook project that I’ll be building off in the future. I hope this will be useful to others trying to get started on developing Outlook add-ins using managed code. Please let me know if I’m missing anything or have errors.

Install Primary Interop Assemblies
.NET can interface with COM code using interop assemblies. You can create these as needed by adding a reference to a COM type library. However, if you do this for the Outlook/Office type libraries, this will lead to strange problems like this. This doesn’t sound like the thing I want to find out about the hard way. The solution is to install “primary interop assemblies” that live in the GAC and will be used instead of custom generated ones. You can download PIAs for Outlook XP here. For Outlook 2003, you can go to Control Panels->Add/Remove programs and customize your installation to add them. Just choose “.NET Programmability” under the various components. They are initially set to install on first use – I don’t know what would actually trigger this. Once these are installed, adding a reference to the COM type libraries will add these “magic” versions instead of generating new versions with strange issues.

Create a Visual Studio.NET project
Create a new Visual Studio.Net project. For the project type, select Other Projects->Extensibility Projects->Shared Add-ins (who would think to look here?). This brings you through a wizard where you can select the language (C#, of course!) and which hosts to support. One cool thing you can do with the Office COM-plugins is support multiple apps with the same plugin, but I’m only interested in Outlook for now. Then, you have the chance to fill in some other random info, and your project is created. The project will have template code that implements the IDTExtensibility2 interface required to create an add-in.

Add references
We need to add references to a couple of things we’ll be using. Right-click References under the add-in project, select “Add Reference”, go to the COM tab, and select Microsoft Outlook 11.0 Object Library (or Outlook 10.0 if you are using Outlook XP). If the PIA stuff worked right, when you select it in the solution explorer, the path in the properties tab should be pointing into the GAC, not into the office folder. Next, select
“Add Reference” again, and add “System.Windows.Forms” from the .NET tab. This will let us do our “Hello World” dialog.

Flesh out code
First, we need to add a member variable. We’ll also change the type of the application object to be the Outlook type (since we’ll only support Outlook):

private Microsoft.Office.Interop.Outlook.Application applicationObject;
private object addInInstance;
private CommandBarButton toolbarButton;

Next, we’ll update OnConnection to cast to the Outlook object type, and add some logic from kb 302901 (why isn’t this in the template if it’s the right thing to do?):

public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{     
    applicationObject = (Microsoft.Office.Interop.Outlook.Application)application;
    addInInstance = addInInst;

    if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
    {
        OnStartupComplete(ref custom);
    }
}

Likewise, we’ll update OnDisconnection according to kb 302901:

public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)
{
    if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)
    {
        OnBeginShutdown(ref custom);
    }
    applicationObject = null;
}

Next, when we’re done loading, we will create a toolbar button. The version in kb 302901 is more complex because it’s generalize to work in apps other than Outlook:

public void OnStartupComplete(ref System.Array custom)
{
    CommandBars commandBars = applicationObject.ActiveExplorer().CommandBars;

    // Create a toolbar button on the standard toolbar that calls ToolbarButton_Click when clicked
    try
    {
         // See if it already exists
         this.toolbarButton = (CommandBarButton)commandBars["Standard"].Controls["Hello"];
    }
    catch(Exception)
    {
        // Create it
        this.toolbarButton = (CommandBarButton)commandBars["Standard"].Controls.Add(1, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
        this.toolbarButton.Caption = "Hello";
        this.toolbarButton.Style = MsoButtonStyle.msoButtonCaption;
    }
    this.toolbarButton.Tag = "Hello Button";
    this.toolbarButton.OnAction = "!<MyAddin1.Connect>";
    this.toolbarButton.Visible = true;
    this.toolbarButton.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(this.OnToolbarButtonClick);
}

On shutdown, we&#8217;ll delete our toolbar button:
public void OnBeginShutdown(ref System.Array custom)
{
    this.toolbarButton.Delete(System.Reflection.Missing.Value);
    this.toolbarButton = null;
}

And, we&#8217;ll define the action when clicking the button:
private void OnToolbarButtonClick(CommandBarButton cmdBarbutton,ref bool cancel)
{
    System.Windows.Forms.MessageBox.Show("Hello World","My Addin");
}

To test it out, you build the addin project, and then the setup project. Quit Outlook, then right-click the setup project and select &#8220;Install&#8221;.  When you launch Outlook, a button named &#8220;Hello&#8221; will show up in the main toolbar. Selecting it will say &#8220;Hello World&#8221;. You can manage this add-in by going to the COM add-in dialog at Tools->Options->Other->Advanced Options->COM Add-Ins.

What&#8217;s missing
There are some steps that need to be taken to install the PIA when installing your add-in. See the steps here. That sample also has a lot of information about signing your plugin, which I&#8217;ve ignored so far.

What&#8217;s next
Next, I have to learn more about the Outlook object model and how to actually do interesting things. I also need to learn how to debug the add-ins.

Reference
General description of COM Add-Ins: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/modcore/html/deovrWhatIsCOMAddin.asp
A sample Visual Basic.NET plugin (describes the PIA stuff): http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnout2k2/html/odc_oladdinvbnet.asp
KB 302901 (building an Office COM plugin using Visual C#.NET): http://support.microsoft.com/?kbid=302901
Niobe, a library for Outlook managed plug-ins (I&#8217;m not sure what you get above doing it from scratch, there isn&#8217;t much documentation): http://www.gotdotnet.com/community/workspaces/workspace.aspx?ID=E7071B93-7970-4962-A4C2-D72AA2CFBCFF

http://weblogs.asp.net/dancre/aggbug/93712.aspx
From : http://weblogs.asp.net/dancre/archive/2004/03/21/93712.aspx

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-112935-1-1.html 上篇帖子: Outlook Express 错误代码表 下篇帖子: outlook 用宏发邮件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表