原创文章转载请注明出处:@协思, 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内核:kthread_create(线程)、SLEEP_MILLI_SEC

    转自:http://blog.csdn.net/guowenyan001/article/details/39230181 一.代码 #include <linux/module.h> # ...

  2. 【转】SqlServer将没有log文件的数据库文件附加到服务器中

    原文链接: http://www.cnblogs.com/xdotnet/p/attach_sqlserver_database_file_without_log_files.html 原作者删除了原 ...

  3. svn: how to set the executable bit on a file?

    http://stackoverflow.com/questions/17846551/svn-how-to-set-the-executable-bit-on-a-file svn uses pro ...

  4. asterisk 通话噪音,自动挂断,回声等情况

    打开配置文件:cd /etc/asterisk/ vim chan_dahdi.conf 1: busydetect:忙音检测,如果开启,Asterisk会拨号尝试或通话中分析在线的音频,从而尝试识别 ...

  5. js获取css中的样式

    众所周知,obj.style只能够获取 <div id="a" style="width:100px;"></div> 结构上的样式 如 ...

  6. DataFrame格式化

    1.如果是格式化成Json的話直接 val rdd = df.toJSON.rdd 2.如果要指定格式需要自定义函数如下: //格式化具体字段条目 def formatItem(p:(StructFi ...

  7. 自动滑动的banner图

    实例: HTML页面: <div style="position: absolute; left: 0; top: 0; width: 100%; height: 100%; min- ...

  8. 盘点销售一体机 打印POS一体的设备。 打印,盘点,销售PDA(手持终端)+移动销售POS软件

    一.产品介绍 本产品为一个PDA(手持终端)+移动销售POS管理软件组合.可以实现功能如下: 可以实现移动销售(销售退货).盘点.配送.移库.打印小票的功能. 销售时,可以选择客户.业务员.库房,并且 ...

  9. 2016-2017 ACM-ICPC Asia-Bangkok Regional Contest

    A. WSI Extreme 将人按洗澡时间从大到小排序,那么$ans=\sum_{i=1}^{n}a_i\times\lfloor\frac{i+W-1}{W}\rfloor$. 当$W$比较大时, ...

  10. pair的使用

    #include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...