小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

OFFICE 的加載項(xiàng)(add in)(轉(zhuǎn))

 夢中家園 2013-01-20

OFFICE 的加載項(xiàng)(add in)(轉(zhuǎn))  

2012-04-23 12:01:18|  分類: MFC |字號(hào) 訂閱

原文:http://www.cnblogs.com/brooks-dotnet/archive/2010/03/01/1675324.html

一、加載項(xiàng)簡介

Office提供了多種用于擴(kuò)展Office應(yīng)用程序功能的模式,常見的有:

1、Office 自動(dòng)化程序(Automation Executables)

2、Office加載項(xiàng)(COM or Excel Add-In)

3、Office文檔代碼或模板(Code Behind an Office Document or Template)

4、Office 智能標(biāo)簽(Smart Tags)

本次我們將學(xué)習(xí)使用VSTO 4.0編寫一個(gè)簡單的Office COM 加載項(xiàng),構(gòu)建于Office 2010 x64.

示例代碼下載

本系列所有示例代碼均在 Visual Studio 2010 Ultimate RC 和 Office 2010 Professional Plus Beta x64 中測試通過。

二、為什么要使用加載項(xiàng)

Office加載項(xiàng)提供了一種擴(kuò)展應(yīng)用程序核心功能的機(jī)制,由此添加的功能可以在整個(gè)應(yīng)用程序或單個(gè)應(yīng)用程序中使用,極大的擴(kuò)充了Office的應(yīng)用領(lǐng)域:

1、擴(kuò)展現(xiàn)有功能:對于特定需求,特別是和業(yè)務(wù)緊密相關(guān)的需求,如果能夠以加載項(xiàng)的方式在Office中解決,那么將節(jié)省軟件成本、培訓(xùn)成本等。

2、數(shù)據(jù)整合:隨著Office由辦公平臺(tái)正式轉(zhuǎn)變?yōu)橐粋€(gè)計(jì)算平臺(tái)之后,將Office與其他平臺(tái)進(jìn)行整合的需求也變得愈來愈頻繁。如將Web服務(wù)器中數(shù)據(jù)導(dǎo)入到Excel中,將SAP系統(tǒng)中數(shù)據(jù)導(dǎo)入到Excel中,生成日報(bào)表等。

三、COM加載項(xiàng)的工作原理

要使用COM加載項(xiàng),必須要在注冊表中寫入相應(yīng)的信息。Office 要判斷使用哪些加載項(xiàng)時(shí),以Excel為例,需要查看注冊表中的兩個(gè)位置:

(PS:推薦一個(gè)注冊表編輯器Registry Workshop,功能很強(qiáng)大,支持64位,可以在這里下載)

1、HKEY_CURRENT_USER\Software\Microsoft\Office\Excel\AddIns

這個(gè)位置是針對于特定用戶的,也是推薦的位置。在該位置注冊的COM加載項(xiàng)將出現(xiàn)在Office中的COM加載項(xiàng)對話框中:

在該位置,有幾項(xiàng)是必備的。

1)FriendlyName:字符串值,包含了顯示在COM對話框中的COM加載項(xiàng)的名稱;

2)Description:字符串值,包含了COM加載項(xiàng)的簡短描述信息;

3)LoadBehavior:DWORD類型,用于描述COM加載項(xiàng)的加載方式,通常設(shè)置為3(1 + 2).


描述

0

斷開,不加載COM加載項(xiàng)

1

連接,加載 COM加載項(xiàng)

2

啟動(dòng)時(shí)加載,主應(yīng)用程序啟動(dòng)時(shí)加載并連接COM加載項(xiàng)

8

需要時(shí)加載,主應(yīng)用程序需要(觸發(fā)加載事件)時(shí)加載并連接COM加載項(xiàng)

16

首次連接,用戶注冊加載項(xiàng)后,首次運(yùn)行主應(yīng)用程序時(shí)加載并連接COM加載項(xiàng)


除了上面的三個(gè),還需要在HKEY_CLASSES_ROOT\CLSID下創(chuàng)建幾個(gè)注冊表項(xiàng):

2、HKEY_LOCAL_MACHINE\Software\Microsoft\Office\Excel\AddIns

這個(gè)位置是針對于所有用戶的,但是這些加載項(xiàng)對用戶隱藏,即不會(huì)出現(xiàn)在COM加載項(xiàng)對話框中。

這些注冊表項(xiàng)如果純手工寫將會(huì)非常麻煩,而且容易出錯(cuò),幸運(yùn)的是,Visual Studio提供了一組模板來方便的創(chuàng)建Office加載項(xiàng),但是我們還是應(yīng)該理解這些注冊表項(xiàng)及其代表含義。

四、理解IDTExtensibility2接口

所有Office應(yīng)用程序都是用IDTExtensibility2接口與COM加載項(xiàng)進(jìn)行通信,該接口提供了一種通用的初始化機(jī)制,并具有在Office應(yīng)用程序的對象模型中傳遞數(shù)據(jù)的能力,因此COM加載項(xiàng)可以與Office應(yīng)用程序通信。

IDTExtensibility2接口并不復(fù)雜,但是對于標(biāo)注COM加載項(xiàng)的加載順序,以及它在何處影響我們編寫的代碼來說,此接口至關(guān)重要。Office在對COM加載項(xiàng)進(jìn)行實(shí)例化時(shí),會(huì)創(chuàng)建主Connect類,注意不能用Connect的構(gòu)造函數(shù)創(chuàng)建類的實(shí)例,應(yīng)該用OnConnection方法;類似的,加載項(xiàng)的關(guān)閉不能調(diào)用析構(gòu)函數(shù),而要用OnDisconnection方法。

五、ExcelCOMAddInDemo

現(xiàn)在我們來動(dòng)手開發(fā)一個(gè)簡單的Excel COM Add-In.

1、新建一個(gè)Share Add-In項(xiàng)目:

2、在彈出的項(xiàng)目向?qū)е校c(diǎn)擊【Next】:

3、選擇你所熟悉的語言,點(diǎn)擊【Next】:

4、選擇COM 加載項(xiàng)的目標(biāo)宿主,點(diǎn)擊【Next】:

5、輸入Add-In的名稱和描述,點(diǎn)擊【Next】:

6、選擇"啟動(dòng)時(shí)加載",點(diǎn)擊【Next】:

7、點(diǎn)擊【Finish】:

8、項(xiàng)目建立完成后會(huì)自動(dòng)添加一個(gè)安裝項(xiàng)目,它會(huì)負(fù)責(zé)安裝生成的COM加載項(xiàng),處理注冊表的信息等,無需我們手動(dòng)參與。


64-bit Support and Code Compatibility

Office 2010 will ship in both 32- and 64-bit versions. 64-bit is particularly significant to some Excel applications, which hit a wall today in terms of available memory address space. This is an important topic in itself, which we have covered here.

Excel workbooks can be freely opened and edited in both 32- and 64-bit Excel; there is nothing architecture specific in a saved workbook. For custom code solutions, however, 64-bit Excel introduces some challenges:


ActiveX controls need to be ported – they need a 64-bit version to work in a 64-bit process. This includes Microsoft's own controls, of which many have been ported. We are evaluating the popularity and criticality of the remaining ones for possible porting.
COM add-ins, similarly, need to be compiled for 64-bit in order to work in 64-bit Excel.
XLL add-ins also need to be compiled for 64-bit, and the new Excel 2010 XLL SDK supports that.
VBA: Embedded VBA code gets re-compiled when the containing Excel workbook is opened on a new platform; 64-bit Excel now includes a 64-bit version of VBA. So most VBA code just works in 64-bit. However, a subset of VBA solutions needs some tweaking. It has to do with declarations and calls to external APIs with pointers/handles in the parameter list. VBA7, the new version of VBA that ships with Office 2010, supports the development of code that can run in both 32- and 64-bit Office.

摘自Excel 2010官方博客,可以看出,在Office 2010 x64中,必須編寫64位的COM Add-In.

這不是問題,稍后我們將看到如何編譯64位的COM Add-In和生成64位的安裝文件。

9、設(shè)置項(xiàng)目屬性:

在【Build】標(biāo)簽中,將【Platform】設(shè)置為:Active(x64),取消【Register for COM】的選擇:

在【Debug】標(biāo)簽中,將【Platform】設(shè)置為:Active(x64),同時(shí)將【Start Action】設(shè)置為Excel的安裝路徑,這樣做可以啟動(dòng)Excel來調(diào)試COM Add-In:

配置編譯選項(xiàng):

配置安裝項(xiàng)目屬性:

10、我們要做的是在Excel 的Ribbon中添加一個(gè)按鈕,點(diǎn)擊后彈出一則歡迎信息。

首先添加引用:

Microsoft.Office.Interop.Excel

System.Windows.Forms

打開Connect.cs,添加如下代碼:


OFFICE 的加載項(xiàng)(add in)(轉(zhuǎn)) - Au - Au的博客代碼
namespace ExcelCOMAddInDemo
{
using System;
using Extensibility;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Core;
using System.Windows.Forms;
#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 ExcelCOMAddInDemoSetup project,
// right click the project in the Solution Explorer, then choose install.
#endregion
/// <summary>
/// The object for implementing an Add-in.
/// </summary>
/// <seealso class='IDTExtensibility2' />
[GuidAttribute("DDC49E0C-03FE-4134-9829-65EF0351CECE"), ProgId("ExcelCOMAddInDemo.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
private Microsoft.Office.Interop.Excel.Application applicationObject;
private Microsoft.Office.Core.COMAddIn addInInstance;
private CommandBarButton simpleButton;
/// <summary>
/// Implements the constructor for the Add-in object.
/// Place your initialization code within this method.
/// </summary>
public Connect()
{
}
/// <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)
{
applicationObject = application as Microsoft.Office.Interop.Excel.Application;
addInInstance = addInInst as Microsoft.Office.Core.COMAddIn;
}
/// <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)
{
}
/// <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)
{
CommandBars commandBars;
CommandBar standardBar;
commandBars = applicationObject.CommandBars;
// Get the standard CommandBar from Word
standardBar = commandBars["Standard"];
try
{
// try to reuse the button is hasn't already been deleted
simpleButton = (CommandBarButton)standardBar.Controls["Excel COM Addin"];
}
catch (System.Exception)
{
// If it's not there add a new button
simpleButton = (CommandBarButton)standardBar.Controls.Add(1);
simpleButton.Caption = "Excel COM Addin";
simpleButton.Style = MsoButtonStyle.msoButtonCaption;
}
// Make sure the button is visible
simpleButton.Visible = true;
simpleButton.Click += new _CommandBarButtonEvents_ClickEventHandler(simpleButton_Click);
standardBar = null;
commandBars = null;
}
/// <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)
{
}
void simpleButton_Click(CommandBarButton ctrl, ref bool cancelDefault)
{
MessageBox.Show("Welcome to COM Add In");
}
}
}

聲明了一個(gè)CommandBarButton的實(shí)例,然后將其添加到CommandBar中,并關(guān)聯(lián)了一個(gè)Click事件,輸出一則歡迎信息。

11、編譯成功后,安裝生成的setup.exe,執(zhí)行安裝:

12、安裝成功后,打開Excel,會(huì)在【Add-In】中發(fā)現(xiàn)我們新添加的按鈕:

六、調(diào)試COM加載項(xiàng)

1、通過前面的設(shè)置為Excel啟動(dòng)來進(jìn)行調(diào)試,這也是首先的方法,很方便。

2、可以通過附加Excel進(jìn)程的方式來進(jìn)行調(diào)試:

七、小結(jié)

本次主要學(xué)習(xí)了COM 加載項(xiàng)的開發(fā)流程,對使用托管代碼開發(fā)COM組件有了初步的認(rèn)識(shí),尤其是IDTExtensibility2接口,在VSTO開發(fā)中占據(jù)重要地位,理解其與COM交互的過程是很有必要的。其次是64位COM加載項(xiàng)的部署問題,與32位傳統(tǒng)加載項(xiàng)有些區(qū)別,請注意區(qū)別對待。后續(xù)篇章會(huì)繼續(xù)深入介紹VSTO開發(fā)的內(nèi)容,及其與其他技術(shù)的整

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多