• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

用C#写Office插件(ZT)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

前段时间写了个Word的插件,因为项目最终没有谈成,也就没有做完,不过好歹写了点基本代码,记在这里,也免得以后用的时候再浪费时间。用C#写Office活着VS的插件是不困难的,VS提供了向导,会生成一个基本的框架,然后可以使用Office的对象模型加入些工具条按钮什么的,用C#写这些东西很方便,特别是连接事件,比起以前用C++写,真的是方便了很多,这种方便性也使得实现一些复杂的功能变得比较愉快
懒得整理了,把主要的代码贴在这里,其他功能方面的东西和写普通的winform程序也没什么区别。看到一些大虾贴的代码都有语法着色的,偶不会,只好弄成灰的了,要是哪位大虾路过看到,也留个言指点一下在下怎么在blog里使代码有语法着色,小的先谢了。

修改: 根据BirdsHome的方法, 修改成有语法着色的了

using System;

using Microsoft.Office.Core;

using Extensibility;

using System.Runtime.InteropServices;

using System.Windows.Forms;

 

namespace WordExtCS

{

 

     #region Read me for Add-in installation and setup information.

     // When run, the Add-in wizard prepared the registry for the Add-in.

     // At a later time, if the Add-in becomes unavailable for reasons such as:

     //   1) You moved this project to a computer other than which is was originally created on.

     //   2) You chose 'Yes' when presented with a message asking if you wish to remove the Add-in.

     //   3) Registry corruption.

     // you will need to re-register the Add-in by building the MyAddin21Setup project

     // by right clicking the project in the Solution Explorer, then choosing install.

     #endregion

 

     /// <summary>

     ///   The object for implementing an Add-in.

     /// </summary>

     /// <seealso class='IDTExtensibility2' />

     [GuidAttribute("C1C829AE-FCEB-4640-BF88-FF6CF8712426"), ProgId("WordExtCS.Connect")]

     public class Connect : Object, Extensibility.IDTExtensibility2

     {

         /// <summary>

         ///  Implements the constructor for the Add-in object.

         ///  Place your initialization code within this method.

         /// </summary>

          public Connect()

         {

         }

 

         #region IDTExtensibility2 接口实现

         /// <summary>

         ///      Implements the OnConnection method of the IDTExtensibility2 interface.

         ///      Receives notification that the Add-in is being loaded.

         /// </summary>

         /// <param term='application'>

         ///      Root object of the host application.

         /// </param>

         /// <param term='connectMode'>

         ///      Describes how the Add-in is being loaded.

         /// </param>

         /// <param term='addInInst'>

         ///      Object representing this Add-in.

         /// </param>

         /// <seealso class='IDTExtensibility2' />

         public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)

         {

              addInInstance = addInInst;

              if (application is Word.Application)

              {

                   wordApp = (Word.Application)application;

              }

 

              if(connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)

              {

                   OnStartupComplete(ref custom);

              }

 

         }

 

         /// <summary>

         ///     Implements the OnDisconnection method of the IDTExtensibility2 interface.

         ///     Receives notification that the Add-in is being unloaded.

         /// </summary>

         /// <param term='disconnectMode'>

         ///      Describes how the Add-in is being unloaded.

         /// </param>

         /// <param term='custom'>

         ///      Array of parameters that are host application specific.

         /// </param>

         /// <seealso class='IDTExtensibility2' />

         public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom)

         {

              if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown)

              {

                   OnBeginShutdown(ref custom);

              }

              wordApp = null;

         }

 

         /// <summary>

         ///      Implements the OnAddInsUpdate method of the IDTExtensibility2 interface.

         ///      Receives notification that the collection of Add-ins has changed.

         /// </summary>

         /// <param term='custom'>

         ///      Array of parameters that are host application specific.

         /// </param>

         /// <seealso class='IDTExtensibility2' />

         public void OnAddInsUpdate(ref System.Array custom)

         {

         }

 

         /// <summary>

         ///      Implements the OnStartupComplete method of the IDTExtensibility2 interface.

         ///      Receives notification that the host application has completed loading.

         /// </summary>

         /// <param term='custom'>

         ///      Array of parameters that are host application specific.

         /// </param>

         /// <seealso class='IDTExtensibility2' />

         public void OnStartupComplete(ref System.Array custom)

         {

              //添加定制工具条

              try

              {

                   string caption = "投标系统";

                   object missing = System.Reflection.Missing.Value;

 

                   //看看工具条是不是已经存在

                   try

                   {

                       toolBar = wordApp.CommandBars[caption];

                   }

                   catch (Exception)

                   {

                       //如果不存在,创建工具条

                       toolBar = (Microsoft.Office.Core.CommandBar)wordApp.CommandBars.Add(

                            caption, Microsoft.Office.Core.MsoBarPosition.msoBarTop, missing, false);

                       toolBar.Visible = true;

                   }

              }

              catch (Exception e)

              {

                   MessageBox.Show("添加投标系统工具条失败,异常信息:" + e.Message, "Tender System");

              }

 

              //


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
使用C#进行点对点通讯和文件传输(通讯基类部分)发布时间:2022-07-10
下一篇:
C# 打印PDF文档的10种方法发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap