原创文章转载请注明出处:@协思, http://zeeman.cnblogs.com

得益于微软系强大的共通能力和Visual Studio的开发支持,做Office插件不是什么难事。一点经验记录如下:

1. 如果要同时开发Word和Outlook插件,那么可将复用的代码封闭到独立的Library中。

2. 在可安装.NET Framework 4的系统上,可以嵌入WPF组件。

3. 由于Office的安全模型,安装部署里需要可信任证书的签名。

4. 初始化代码可在ThisAddIn添加,如Startup、Shutdown、Application.NewMailEx...

代码集锦


1. 获取文件名:

app = Globals.ThisAddIn.Application;

Path.GetExtension(app.ActiveDocument.FullName)

2.检查文档是否保存:

app = Globals.ThisAddIn.Application;

if (!app.ActiveDocument.Saved)

{

    if (MessageBox.Show("This command publish the disk version of a file to the server. Do you want to save your changes to disk before proceeding?", "warning",

        MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)

    {

        try

        {

            app.ActiveDocument.Save();

            MessageBox.Show("save succeeded", "information", MessageBoxButtons.OK, MessageBoxIcon.Information);

        }

        catch (Exception ex)

        {

            MessageBox.Show("saved failed." + ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            return;

        }

    }

}

3. 获取文档内容,并添加自己的信息

public byte[] GetDocumentContent(Word.Document wordDoc, string headerText, string footerText)

{

    //使用Mail.RTFBody获取文档内容会丢失部分格式,所以这里还是采用剪贴板方式。

    //复制文档内容到剪贴板

    wordDoc.Content.Copy();

    using (RichTextBox rtb = new RichTextBox())

    {

        //添加头部信息

        rtb.AppendText(headerText);

        rtb.SelectAll();

        rtb.SelectionFont = new Font("Courier New", );

        rtb.SelectionColor = Color.Green;

        //添加正文

        rtb.Select(rtb.TextLength, );

        rtb.Paste();

        Clipboard.Clear();

        //添加尾部信息

        rtb.SelectionFont = new Font("Courier New", );

        rtb.SelectionColor = Color.Green;

        rtb.AppendText(footerText);

        using (System.IO.MemoryStream stream = new MemoryStream())

        {

            rtb.SaveFile(stream, RichTextBoxStreamType.RichText);

            return stream.ToArray();

        }

    }

}

4. outlook邮件正文转换为word文档:

object selObject = currentExplorer.Selection[];

MailItem mail = selObject as MailItem;

if (mail == null)

{

    MessageBox.Show("non-mail item not supported.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

    return;

}

Word.Document wordDoc = (Word.Document)mail.GetInspector.WordEditor;

资源下载


Office Control Identifiers: http://www.microsoft.com/en-us/download/details.aspx?id=6627

Office Document Extractor: http://code.msdn.microsoft.com/office/CSOfficeDocumentFileExtract-e5afce86

记录Office Add-in开发经验的更多相关文章

  1. 使用VS2012开发基于Office 2013的AddIn程序

    默认VS2012开发的Office Add是基于2010的,如下所示: 如果你机器上安装的Office版本是2013,那么使用VS2012创建的工程是无法运行的,弹出如下的错误: 那么此时怎么办呢?将 ...

  2. ASP.NET MVC5 网站开发实践(二) Member区域 - 添加文章

    上次把架构做好了,这次做添加文章.添加文章涉及附件的上传管理及富文本编辑器的使用,早添加文章时一并实现. 要点: 富文本编辑器采用KindEditor.功能很强大,国人开发,LGPL开源,自己人的好东 ...

  3. (1-1)文件结构的升级(Area和Filter知识总结) - ASP.NET从MVC5升级到MVC6

    ASP.NET从MVC5升级到MVC6 总目录 MVC5项目结构 带有Areas和Filter的项目结构 一般来说,小的MVC项目是不考虑领域的,但是,如果是稍微复杂一点的项目,往往是需要领域这个概念 ...

  4. Python学习day3作业

    days3作业 作业需求 HAproxy配置文件操作 根据用户输入,输出对应的backend下的server信息 可添加backend 和sever信息 可修改backend 和sever信息 可删除 ...

  5. csc.rsp Nuget MVC/WebAPI、SignalR、Rx、Json、EntityFramework、OAuth、Spatial

    # This file contains command-line options that the C# # command line compiler (CSC) will process as ...

  6. Codeforces 307 div2 E.GukiZ and GukiZiana 分块

    time limit per test 10 seconds memory limit per test 256 megabytes input standard input output stand ...

  7. 总结/PSP初体验—排球计分程序1.0

    要做一个排球计分程序,墨迹了很长时间才做出个的东西,过程很不爽: 功能:这个软件有两个页面,可以实现窗体A的部分变化控制窗体B的部分变化.A是操作人员使用看到的,B是投放给观众的,完全由A操控: 学到 ...

  8. 我的git与github学习历程

    因为想要知道如何把代码放到github上,所以就百度了一下,然后找到一个<如何从github上面拷贝源码>的文章,就先进行练习了下   1.首先到git官网下载git版本控制工具的安装包, ...

  9. 初探Backbone

    Backbone简介 中文API:http://www.csser.com/tools/backbone/backbone.js.html 英文API:http://backbonejs.org/ B ...

随机推荐

  1. 优化Linux内核参数

    转自:http://www.centoscn.com/CentOS/config/2013/0804/992.html vim /etc/sysctl.conf 1.net.ipv4.tcp_max_ ...

  2. Bootstrap UI 编辑器

    1. BootSwatchr BootSwatchr 是由 Drew Strickiand 独立开发和维护的,是唯一支持从右到左语言显示的 Bootstrap 自定义构建工具,这也是它的特色之一.Bo ...

  3. webrtc中APM(AudioProcessing module)的使用2

    这个其实就是从Audio_processing.h中拿出来的. APM should be placed in the signal chain as close to the audio hardw ...

  4. border-radius四个值的问题

    我们都知道border-radius后面如果是4个值的话,依次代表的是左上角.右上角.右下角.左下角. 但是这样写呢:border-radius:10px 20px 30px 40px/50px 40 ...

  5. JS定义数组,初始化

    定义一维数组 方法1: var _TheArray = new Array);/定义 _TheArray[0]="1"; _TheArray[1]="2"; _ ...

  6. mac安装java8

    http://stackoverflow.com/questions/24342886/how-to-install-java-8-on-mac brew tap caskroom/cask brew ...

  7. 【原】iOS学习之PINCache第三方缓存框架

    在项目中总是需要缓存一些网络请求数据以减轻服务器压力,业内也有许多优秀的开源的解决方案.通常的缓存方案都是由内存缓存和磁盘缓存组成的,内存缓存速度快容量小,磁盘缓存容量大速度慢可持久化. 1.PINC ...

  8. 面试中常问的有关随机选取k个数的总结

    1.在半径为1的圆中随机选取一点. 2.给定一个未知长度的整数流,如何随机选取一个数 3.给定一个数据流,其中包含无穷尽的搜索关键字(比如,人们在谷歌搜索时不断输入的关键字).如何才能从这个无穷尽的流 ...

  9. [Leetcode] Contains Duplicate III

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...

  10. 【BZOJ】4144: [AMPPZ2014]Petrol

    题意 给定一个\(n\)个点.\(m\)条边的带权无向图,其中有\(s\)个点是加油站.每辆车都有一个油量上限\(b\),即每次行走距离不能超过\(b\),但在加油站可以补满.\(q\)次询问,每次给 ...