Hello Word Outlook Add-In using C#

Outlook Add-in(COM加载项)技术指南(一) Outlook Add-in(COM加载项)技术指南Revision History:VersionDateCreatorDescription1.0.0.12004-3-9郑昀草稿摘要:本文档详细介绍了编写Outlook Add-in插件的背景知识和开发过程。第一章概述了Outlook Add-in插件的背景知识。第二章详细介绍了Outlook Add-in插件的开发过程。1 Outlook C 阅读详情

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’ll delete our toolbar button:

public void OnBeginShutdown(ref System.Array custom)

{

    this.toolbarButton.Delete(System.Reflection.Missing.Value);

    this.toolbarButton = null;

}

 

And, we’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 “Install”.  When you launch Outlook, a button named “Hello” will show up in the main toolbar. Selecting it will say “Hello World”. You can manage this add-in by going to the COM add-in dialog at Tools->Options->Other->Advanced Options->COM Add-Ins.

 

What’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’ve ignored so far.

 

What’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’m not sure what you get above doing it from scratch, there isn’t much documentation): http://www.gotdotnet.com/community/workspaces/workspace.aspx?ID=E7071B93-7970-4962-A4C2-D72AA2CFBCFF


From : http://weblogs.asp.net/dancre/archive/2004/03/21/93712.aspx
Outlook add-in 插件.Net开发经验 Outlook add-in 插件.Net开发经验第一次写这玩意,记录些开发中的过程,.net开发设置要比在VB里复杂一些,要把office的对象用ms提供的工具包装一下,才能在.Net开发环境里使用。开发环境设置:我的开发环境:Windows2K professional + Office XP Office每个版本的对象,不尽相同,一般新版本兼容旧版本,也有可能会废弃某些对 阅读详情

相关推荐

M365 Copilot 是企业级 Agent 系统,不是 Copilot 按钮

Agent(智能体)是现代办公自动化的核心范式,指具备目标分解、工具调用、状态记忆、错误恢复与权限沙盒能力的自主执行单元。它超越传统插件和宏,通过深度集成Microsoft Graph API与M365原生服务,在真实协作流中实现跨应用上下文感知与主动协同。其技术价值在于将碎片化办公动作升维为可编排、可治理、可审计的自动化流水线,广泛应用于销售线索分发、合同审查、IT自助服务等高ROI场景。本文聚焦M365 Copilot作为典型企业级Agent系统的架构本质与落地实践,深入解析Agent沙盒权限配置、生命

aobu0171的博客 287

C# Outlook 示例

C#调用Outlook功能,实现Outlook功能的补充

C# Outlook

C#编写的一个Outlook客户端!<br>支持附件!<br>03环境!

[转载]Hello World Outlook Add-In using C#

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 thin...

weixin_33924770的博客 197

今天没事,看到一个用C#开发OutLook插件的例子,顺便自己做了一个

C#开发OutLook插件引言:我们可以用VS.Net 2003开发OutLook插件,把自己的代码集成到OutLook中去。比如说我们可以在简单做一个邮件的统计功能。软件原理:利用C#调用Office的接口代码实现:1. 我们新建一个addin项目此主题相关图片如下:此主题相关图片如下:此主题相关图片如下:此主题相关图片如下:这样我们根据.net的向导就自动生成了插件项目。2. 如果你的应用程...

weixin_34080903的博客 2155

[转载]Hello Word Outlook Add-In using C#

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 th...

weixin_30325793的博客 221

NetOffice框架终极指南:如何轻松扩展与自动化Microsoft Office应用

NetOffice是一个强大的框架,它允许开发者轻松扩展和自动化Microsoft Office应用程序。无论是创建Excel插件、Word宏还是Outlook扩展,NetOffice都提供了简单易用的API和丰富的功能,帮助开发者快速实现各种Office自动化任务。 ## 什么是NetOffice框架? NetOffice是一个开源的.NET框架,它提供了对Microsoft Office应

gitblog_01159的博客 247

微软技术周报 · 2026-08-10

微软技术周报摘要 · 2026-08-10 本周微软技术生态聚焦三大主线: 1. .NET 11蓄势待发 • .NET 11 Preview 7即将发布,带来Blazor输出缓存、QuickGrid虚拟化等ASP.NET Core改进 • MCP C# SDK 2.0实现无状态HTTP化,支持水平扩展和多轮交互请求 • C# 14扩展成员和field关键字落地,C# 15联合类型特性开始预览 2. AI基础设施全面升级 • VS 2026内置.NET/Azure Agent技能 • Copilot Stud

海盗Sharp的博客 697

微软技术周报 · 2026-08-11~2026-08-18

微软本周技术动态聚焦两大方向:AI体验整合与安全更新。AI方面,Copilot统一入口copilot.cloud.microsoft上线,Foundry Agent框架GA,MAI模型家族完成5线整合;开发者工具上,C# 14正式支持扩展成员、field关键字等特性,.NET 11 Preview 7优化异步分层编译和WASM支持。安全侧发布8月补丁日,修复421个CVE(含1个Lazarus组织利用的零日漏洞)。其他亮点包括VS Code接入MAI-Voice、Power Platform现代控件GA、T

海盗Sharp的博客 645

Windows智能体开发实战:从API调用到能力集成的系统级Agent构建

在实际技术演进中,操作系统与AI智能体的关系正从简单的“运行平台”向“深度融合的基础设施”转变。微软Build 2026所预示的“Windows成为智能体的‘一等公民’”,并非一个遥远的营销概念,而是开发者即将面对的技术现实。这意味着智能体(Agent)将能更原生地调用系统资源、感知用户上下文、执行复杂任务,而不再仅仅是一个运行在浏览器或独立应用中的“外挂”程序。对于开发者而言,理解并掌握如何在这种新范式下构建、调试和部署智能体,将成为一项核心技能。 本文将从工程实践角度,探讨在Windows作为智能体“一

weixin_30718391的博客 382

Outlook 插件开发小结

  最近实习在做outlook插件开发,阅读了一些VSTO的相关概念和知识。遂将整理所得与大家分享和交流。PS:这篇博客为本人的第一篇正式技术博客,如有错误和不妥之处请读者见谅。     I.基本介绍   1.VSTO外接程序体系结构   2.Outlook add-in注册表项   1.Microsoft Office 2010 应用程序可加载在...

weixin_34345753的博客 1290

[Rosyln学习记录2]添加引用

C#程序时,我们需要关注当前程序所需要的引用(Reference)。只有这些引用被添加进编译器,其在编译的时候才能够找到我们在程序中使用的程序集

qqdown1993的博客 1040

【转】基于Web技术的Outlook Add-ins开发简介

基于Web技术的Outlook Add-ins开发简介   我也是刚刚接触Outlook Add-ins的开发,水平有限;文中若有错误,拍砖请轻一些!   零、关于Outlook Add-ins   Outlook插件的种类不止一种,早期的有基于COM技术的、基于VSTO(VisualStudio Tools for Office)的,还有就是这里介绍的基于Web技术实现的方式。微软

deng0jun的专栏 6022

HOW TO:使用 Visual C# .NET 生成 Office COM 外接程序

察看本文应用于的产品function loadTOCNode(){} 文章编号 : 302901 最后修改 : 2006年3月31日

电子商务 asp.net ajax 1080
上一篇: 超级爆笑:糟了,我怀孕了
下一篇: 到底怎样安装/初始化/个性化DotNetNuke(DNN)?
陈亚平
博客等级 码龄23年 47粉丝 85原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值