原创文章转载请注明出处:@协思, 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. 私有无线传感网 PWSN HLINK

    私有无线传感网,我把其叫做 Personal Wireless Sensor Network.此种网络最另众人所知的就是ZIGBEE了.由于在用户不同的使用场景中,对传感网络有许多不同的要求,例如:通 ...

  2. apache的AB测试

    A/B测试A/B测试是一种新兴的网页优化方法,可以用于增加转化率注册率等网页指标..A/B测试的目的在于通过科学的实验设计.采样样本代表性.流量分割与小流量测试等方式来获得具有代表性的实验结论,并确信 ...

  3. 安装maven编译环境

    安装maven编译环境 1.默认已经装好yum并配置好yum源(推荐使用163yum源) 2.安装JDK 3.安装相关依赖环境(root用户登陆) yum install -y cmake lzo-d ...

  4. SSH框架整合(全注解)

    全部jar包    目录结构  配置案例 package cn.yzu.Tbook.action; import javax.annotation.Resource; import org.apach ...

  5. Javascript原型继承 __proto__

    Javascript继承是通过原型链继承的 原型链是依赖__proto__而不是prototype var animal = function(){}; var dog = function(){}; ...

  6. 关于UnsupportedClassVersionError的错误处理

    错误:Java.lang.UnsupportedClassVersionError: Bad version number in .class file 造成这种错误的原因是你的支持Tomcat运行的 ...

  7. Codeforces Round #366 (Div. 2)

    CF 复仇者联盟场... 水题 A - Hulk(绿巨人) 输出love hate... #include <bits/stdc++.h> typedef long long ll; co ...

  8. su root认证失败的解决方法

    sudo passwd 输入安装密码. 输入新密码. 输入 su 即获得root权限.

  9. C++-数据库【1】-C++连接MSSQL数据库

    测试环境—— 系统:Win7 64bit 编译器:VC++ 2015 数据库:MSSQL 2008 R2 #include <Windows.h> #include <stdio.h ...

  10. QuartZ Cron表达式

     Cron Expressions cron的表达式是字符串,实际上是由七子表达式,描述个别细节的时间表.        Seconds        Minutes        Hours     ...