用C#写Office插件

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");
              }

              //添加按钮
              try
              {
                   btnConnect = AddButton("连接", 186, new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(connect_Click));
                   comboSearch = AddComboBox("查找内容", new Microsoft.Office.Core._CommandBarComboBoxEvents_ChangeEventHandler(search_Change));
                   btnSearch = AddButton("查找", 46, new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(search_Click));
                   btnAdvanced = AddButton("高级", 162, new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(advanced_Click));
              }
              catch (Exception)
              {
                   MessageBox.Show("添加按钮失败");
              }

              UpdateUI();
         }

         /// <summary>
         ///      Implements the OnBeginShutdown method of the IDTExtensibility2 interface.
         ///      Receives notification that the host application is being unloaded.
         /// </summary>
         /// <param term='custom'>
         ///      Array of parameters that are host application specific.
         /// </param>
         /// <seealso class='IDTExtensibility2' />
         public void OnBeginShutdown(ref System.Array custom)
         {
              //toolBar.Delete();
         }
         #endregion

         #region 辅助函数
         /// <summary>
         /// 添加一个按钮,并指定点击处理函数
         /// </summary>
         Microsoft.Office.Core.CommandBarButton AddButton(string caption, int faceID,Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler clickHandler)
         {
              object missing = System.Reflection.Missing.Value;
              try
              {
                   Microsoft.Office.Core.CommandBarButton button;
                   //看看按钮是否已经存在
                   try
                   {
                       button = (Microsoft.Office.Core.CommandBarButton)toolBar.Controls[caption];
                   }

                   catch
                   {
                       //如果不存在,创建按钮
                       button = (Microsoft.Office.Core.CommandBarButton)toolBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton,missing, missing, missing, missing);
                   }

                   button.Caption = caption;
                   button.Tag  = caption;
                   button.OnAction = "!<WordExtCS.Connect>";
                   button.TooltipText = caption;
                   button.FaceId  = faceID;
                   button.Click   += clickHandler;

                   return button;
              }
              catch (Exception)
              {
                   MessageBox.Show("Error adding button: " + caption, "Error");

                   return null;
              }
         }

         /// <summary>
         /// 添加一个下拉框,并指定点击处理函数
         /// </summary>
         Microsoft.Office.Core.CommandBarComboBox AddComboBox(string caption,Microsoft.Office.Core._CommandBarComboBoxEvents_ChangeEventHandler comboHandler)
         {
              object missing = System.Reflection.Missing.Value;

              try
              {
                   Microsoft.Office.Core.CommandBarComboBox combo;
                   //看看combo是否已经存在
                   try
                   {
                       combo = (Microsoft.Office.Core.CommandBarComboBox)toolBar.Controls[caption];
                   }
                   catch
                   {
                       //如果不存在,创建
                       combo = (Microsoft.Office.Core.CommandBarComboBox)toolBar.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlComboBox,missing, missing, missing, missing);
                   }

                   combo.Caption   = caption;
                   combo.Tag  = caption;
                   combo.TooltipText = caption;
                   combo.OnAction = "!<WordExtCS.Connect>";
                   combo.Change   += comboHandler;

                   return combo;
              }
              catch (Exception)
              {
                   MessageBox.Show("Error adding combo: " + caption, "Error");

                   return null;
              }
         }

          /// <summary>
         /// 根据当前状态更新工具条界面
         /// </summary>
         private void UpdateUI()
         {
              if (connected)
              {
                   btnConnect.TooltipText = "断开";
                   btnConnect.FaceId  = 184;
                   comboSearch.Enabled  = true;
                   btnSearch.Enabled  = true;
                   btnAdvanced.Enabled  = true;
              }
              else
              {
                   btnConnect.TooltipText = "连接";
                   btnConnect.FaceId  = 186;
                   comboSearch.Enabled  = false;
                   btnSearch.Enabled  = false;
                   btnAdvanced.Enabled  = false;
              }
         }
         #endregion

         #region 事件处理函数
         /// <summary>
         /// 连接按钮相应函数
         /// </summary>
         public void connect_Click(Microsoft.Office.Core.CommandBarButton btn, ref bool someBool)
         {
              if (!connected)
              {
                   //建立连接
                   using (LoginForm dlg = new LoginForm())
                   {
                       if (dlg.ShowDialog()==DialogResult.OK)
                       {
                            //...
                            connected = true;
                       }
                   }
              }
              else
              {
                   //断开连接
                   connected = false;
              }

              UpdateUI();
         }


         public void search_Change(Microsoft.Office.Core.CommandBarComboBox combo)
         {
              combo.AddItem(combo.Text, 0);
              //wordApp.ActiveWindow.Selection.InsertBefore(combo.Text);
         }

         public void search_Click(Microsoft.Office.Core.CommandBarButton btn, ref bool someBool)
         {
              using (SearchResult resultForm = new SearchResult())
              {
                   if (resultForm.ShowDialog() == DialogResult.OK)
                   {

                   }
              }
         }

         public void advanced_Click(Microsoft.Office.Core.CommandBarButton btn, ref bool someBool)
         {
              mainForm.ShowDialog();
         }
         #endregion


         //private object applicationObject;
         private Word.Application wordApp;
         private Microsoft.Office.Core.CommandBar toolBar;
         private Microsoft.Office.Core.CommandBarButton btnConnect;
         private Microsoft.Office.Core.CommandBarButton btnSearch;
         private Microsoft.Office.Core.CommandBarComboBox comboSearch;
         private Microsoft.Office.Core.CommandBarButton btnAdvanced;
         private object addInInstance;
         private bool connected = false;
         private TenderMain mainForm = new TenderMain();
     }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、Office2000 下内部COM插件的编程实现.................................................................................2 1.1、版权声明..........................................................................................................................2 1.2、内容详情..........................................................................................................................2 2、用VC6.0 编Word插件..........................................................................................................11 2.1、版权声明........................................................................................................................11 2.2、内容详情........................................................................................................................11 3、探索 Word 2007 开发.............................................................................................................19 3.1、版权声明........................................................................................................................19 3.2、内容详情........................................................................................................................19 3.2.1 我的博客...............................................................................................................19 3.2.2 扩展 Ribbon.........................................................................................................28 3.2.3 管理侧栏...............................................................................................................43 3.2.4 上传图片...............................................................................................................49 3.2.5 部署插件...............................................................................................................56 4、用VC6.0 编Word插件(Office2007 篇).................................................................................66 4.1、版权声明........................................................................................................................66 4.2、内容详情........................................................................................................................66 5、Microsoft Word 语法高亮插件(v1.2) ................................................................................70 5.1、版权声明........................................................................................................................70 5.2、内容详情........................................................................................................................70 6、VSTO学习笔记........................................................................................................................75 6.1、版权声明........................................................................................................................75 6.2、内容详情........................................................................................................................75 6.2.1 VSTO概述.............................................................................................................75 6.2.2 Excel对象模型.......................................................................................................89 6.2.3 开发Office 2010 64 位COM加载项...................................................................101 6.2.4 从SharePoint 2010 中下载文件.........................................................................117 6.2.5 批量编辑Excel 2010 x64....................................................................................123 6.2.6 在 Excel 2010 中使用RDLC报表.....................................................................131 7、Excel 二次开发系列..............................................................................................................137 7.1、版权声明......................................................................................................................137 7.2、内容详情......................................................................................................................137 7.2.1 Excel 编成模型...................................................................................................137 7.2.2 Excel 常用操作(创建、打开、读取、入)...............................................139 7.2.3 创建Excel二次开发环境....................................................................................142 7.2.4 操作一个已经存在Excel....................................................................................143 7.2.5 插件开发系列操作.............................................................................................145 7.2.6 引用Excel模板....................................................................................................172 7.2.7 报表服务基础.....................................................................................................174 7.2.8 报表服务实例.....................................................................................................178
项目使用VS2017打开,.net 2.0下运行。 项目使用的微软官方的插件方法,可以将doc, docx, xls, xlsx, ppt, pptx文件转换为pdf文件,但是需要: 1、用户需要首先安装一个SaveAsPDFandXPS.exe的工具; 2、如果用户是xp系统,则: 2.1 如果用户安装的是office 2007,则用户在安装office 2007的时候必须要安装Visual Basic for Application 和 Microsoft Office Document Imaging这2个选项,否则转换失败; 2.2 如果用户安装的是office 2010,则在安装office 2010时必须要安装Visual Basic for Application选项,然后从office 2007安装包里面安装Microsoft Office Document Imaging(因为2010删除了这个选项,好麻烦~),否则转换失败; 2.3 xp不能安装office 2013/2016; 3、如果用户是win7系统,则: 3.1 如果用户安装的是office 2007,则用户在安装office 2007的时候必须要安装Visual Basic for Application 和 Microsoft Office Document Imaging这2个选项,否则转换失败; 3.2 如果用户安装的是office 2010,则在安装office 2010时必须要安装Visual Basic for Application选项(win7 + office 2010不需要安装Microsoft Office Document Imaging) 3.3 如果用户安装的是office 2013或2016,则不需要额外选项; 4、如果用户是win10系统,则: 4.1 如果用户安装的是office 2007,则用户在安装office 2007的时候必须要安装Visual Basic for Application这个选项,(win10 + office 2007不需要安装Microsoft Office Document Imaging)否则转换失败; 4.2 如果用户安装的是office 2010,则在安装office 2010时必须要安装Visual Basic for Application选项(win10 + office 2010不需要安装Microsoft Office Document Imaging) 4.3 如果用户安装的是office 2013或2016,则不需要额外选项; 5、如果用户安装了wps 2016或者wps 2019也可以正常转换。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值