用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
///
/// The object for implementing an Add-in. ///
///
[GuidAttribute(\ public class Connect : Object, Extensibility.IDTExtensibility2 {
///
/// Implements the constructor for the Add-in object. /// Place your initialization code within this method. /// public Connect() { }
#region IDTExtensibility2 接口实现 ///
/// Implements the OnConnection method of the IDTExtensibility2 interface. /// Receives notification that the Add-in is being loaded. ///
///
/// Root object of the host application. ///
///
/// Describes how the Add-in is being loaded. ///
///
/// Object representing this Add-in. ///
///
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); }
}
///
/// Implements the OnDisconnection method of the IDTExtensibility2 interface. /// Receives notification that the Add-in is being unloaded. ///
///
/// Describes how the Add-in is being unloaded. ///
///
/// Array of parameters that are host application specific. ///
///
public void OnDisconnection(Extensibility.ext_DisconnectMode disconnectMode, ref System.Array custom) {
if(disconnectMode != Extensibility.ext_DisconnectMode.ext_dm_HostShutdown) {
OnBeginShutdown(ref custom); }
wordApp = null;
}
///
/// Implements the OnAddInsUpdate method of the IDTExtensibility2 interface. /// Receives notification that the collection of Add-ins has changed. ///
///
/// Array of parameters that are host application specific. ///
///
public void OnAddInsUpdate(ref System.Array custom) { }
///
/// Implements the OnStartupComplete method of the IDTExtensibility2 interface. /// Receives notification that the host application has completed loading. ///
///
/// Array of parameters that are host application specific. ///
///
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(\添加投标系统工具条失败,异常信息:\System\ }
//添加按钮 try {
btnConnect = AddButton(\
连
接
\
186, new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(connect_Click)); comboSearch = AddComboBox(\ btnSearch = AddButton(\
查查
找
找
内
容\
\new Microsoft.Office.Core._CommandBarComboBoxEvents_ChangeEventHandler(search_Change)); 46, new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(search_Click)); btnAdvanced = AddButton(\ }
catch (Exception) {
MessageBox.Show(\添加按钮失败\ }
UpdateUI();
}
///
/// Implements the OnBeginShutdown method of the IDTExtensibility2 interface. /// Receives notification that the host application is being unloaded. ///
///
/// Array of parameters that are host application specific. ///
///
public void OnBeginShutdown(ref System.Array custom) {
//toolBar.Delete(); }
#endregion
#region 辅助函数 ///
高
级
\
162, new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(advanced_Click));
/// 添加一个按钮,并指定点击处理函数 ///
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 = \ button.TooltipText = caption; button.FaceId = faceID; button.Click += clickHandler; return button; }
catch (Exception) {
MessageBox.Show(\ return null; } }
///
/// 添加一个下拉框,并指定点击处理函数 ///
Microsoft.Office.Core.CommandBarComboBox AddComboBox( string caption,