原创文章转载请注明出处:@协思, 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. fat32转ntfs

    convert c: /fs:ntfs 下了个维基的zim,7G,fat32放不下 :( Microsoft Windows [版本 6.1.7600] 版权所有 (c) 2009 Microsoft ...

  2. SQL SERVER 得到汉字首字母函数四版全集 --【叶子】

    --创建取汉字首字母函数(第三版) create function [dbo].[f_getpy_V3] ( ) ) ) as begin ),) ,@len = len(@col),@sql = ' ...

  3. APPCAN开发笔记:html页面之间的参数传递:使用js获取url中的参数,以及在APPCAN中不能使用的解决方法

    用PHP的GET/POST方式来传递方式已经是司空见惯了,但是如果我的页面是一个静态的html的页面,想传递参数的时候要怎么办呢?在APPCAN的开发中我们会经常遇到这样的问题,因为所有的页面都是静态 ...

  4. jquery 回车事件

    简单地记下jquery实现回车事件,代码如下: 全局: $(document).keydown(function(e){ if(e.keyCode==13){ $(".login-li in ...

  5. mac 安装brew

    安装了xcode ,直接执行以下代码 ruby -e "$(curl -fsSL --insecure https://raw.githubusercontent.com/Homebrew/ ...

  6. 原生js实现查询天气的小应用

    demo:https://zsqosos.github.io/weather/ 截图: 实现功能:打开网页时显示用户所在城市的天气状况,在输入框输入城市可查询其它城市. 实现过程:先调用百度地图的AP ...

  7. 530 User cannot log in, home directory inaccessible.

    服务器是winserver,控制面板-用户账号里新建了一个Ftp账户用来做ftp连接.可在本地连接FTP总提示530 User cannot log in, home directory inacce ...

  8. [Leetcode] Bulls and Cows

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

  9. C语言_第五章__实践(密码转换)

    1.   要求 输入China  输出 Glmre #include <stdio.h> #include <stdlib.h> int main() { char c ; c ...

  10. BZOJ4262: Sum

    Description   Input 第一行一个数 t,表示询问组数. 第一行一个数 t,表示询问组数. 接下来 t 行,每行四个数 l_1, r_1, l_2, r_2.   Output 一共 ...