<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      VSTO學習筆記(三) 開發Office 2010 64位COM加載項

      一、加載項簡介

      Office提供了多種用于擴展Office應用程序功能的模式,常見的有:

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

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

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

      4、Office 智能標簽(Smart Tags)

      本次我們將學習使用VSTO 4.0編寫一個簡單的Office COM 加載項,構建于Office 2010 x64.

      示例代碼下載

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

       

      二、為什么要使用加載項

      Office加載項提供了一種擴展應用程序核心功能的機制,由此添加的功能可以在整個應用程序或單個應用程序中使用,極大的擴充了Office的應用領域:

      1、擴展現有功能:對于特定需求,特別是和業務緊密相關的需求,如果能夠以加載項的方式在Office中解決,那么將節省軟件成本、培訓成本等。

      2、數據整合:隨著Office由辦公平臺正式轉變為一個計算平臺之后,將Office與其他平臺進行整合的需求也變得愈來愈頻繁。如將Web服務器中數據導入到Excel中,將SAP系統中數據導入到Excel中,生成日報表等。

       

      三、COM加載項的工作原理

      要使用COM加載項,必須要在注冊表中寫入相應的信息。Office 要判斷使用哪些加載項時,以Excel為例,需要查看注冊表中的兩個位置:

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

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

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

      在該位置,有幾項是必備的。

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

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

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

      描述

      0

      斷開,不加載COM加載項

      1

      連接,加載 COM加載項

      2

      啟動時加載,主應用程序啟動時加載并連接COM加載項

      8

      需要時加載,主應用程序需要(觸發加載事件)時加載并連接COM加載項

      16

      首次連接,用戶注冊加載項后,首次運行主應用程序時加載并連接COM加載項

       

      除了上面的三個,還需要在HKEY_CLASSES_ROOT\CLSID下創建幾個注冊表項:

       

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

      這個位置是針對于所有用戶的,但是這些加載項對用戶隱藏,即不會出現在COM加載項對話框中。

       

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

       

      四、理解IDTExtensibility2接口

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

      IDTExtensibility2接口并不復雜,但是對于標注COM加載項的加載順序,以及它在何處影響我們編寫的代碼來說,此接口至關重要。Office在對COM加載項進行實例化時,會創建主Connect類,注意不能用Connect的構造函數創建類的實例,應該用OnConnection方法;類似的,加載項的關閉不能調用析構函數,而要用OnDisconnection方法。

       

      五、ExcelCOMAddInDemo

      現在我們來動手開發一個簡單的Excel COM Add-In.

      1、新建一個Share Add-In項目:

      2、在彈出的項目向導中,點擊【Next】:

      3、選擇你所熟悉的語言,點擊【Next】:

      4、選擇COM 加載項的目標宿主,點擊【Next】:

       

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

      6、選擇"啟動時加載",點擊【Next】:

      7、點擊【Finish】:

      8、項目建立完成后會自動添加一個安裝項目,它會負責安裝生成的COM加載項,處理注冊表的信息等,無需我們手動參與。

      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、設置項目屬性:

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

      在【Debug】標簽中,將【Platform】設置為:Active(x64),同時將【Start Action】設置為Excel的安裝路徑,這樣做可以啟動Excel來調試COM Add-In:

      配置編譯選項:

      配置安裝項目屬性:

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

      首先添加引用:

      Microsoft.Office.Interop.Excel

      System.Windows.Forms

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

       

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

      聲明了一個CommandBarButton的實例,然后將其添加到CommandBar中,并關聯了一個Click事件,輸出一則歡迎信息。

       

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

       

      12、安裝成功后,打開Excel,會在【Add-In】中發現我們新添加的按鈕:

       

      六、調試COM加載項

      1、通過前面的設置為Excel啟動來進行調試,這也是首先的方法,很方便。

       

       

      2、可以通過附加Excel進程的方式來進行調試:

       

       

      七、小結

      本次主要學習了COM 加載項的開發流程,對使用托管代碼開發COM組件有了初步的認識,尤其是IDTExtensibility2接口,在VSTO開發中占據重要地位,理解其與COM交互的過程是很有必要的。其次是64位COM加載項的部署問題,與32位傳統加載項有些區別,請注意區別對待。后續篇章會繼續深入介紹VSTO開發的內容,及其與其他技術的整合。

       

      posted @ 2010-03-01 00:25  江蘇瑞步科技  閱讀(19971)  評論(20)    收藏  舉報
      主站蜘蛛池模板: 国产卡一卡二卡三免费入口| 日本成本人片免费网站| 天天看片视频免费观看| 中文字幕无码不卡在线| 国产超碰人人做人人爰| 国产69久久精品成人看| 亚洲精中文字幕二区三区| 无码福利写真片视频在线播放| 国产女人喷潮视频免费| 国产欧美精品一区二区三区-老狼| 国产熟女av一区二区三区| 激情亚洲专区一区二区三区| 中文 在线 日韩 亚洲 欧美| 漂亮人妻被中出中文字幕| 国产福利微视频一区二区| 日本视频一区二区三区1| 日日噜噜噜夜夜爽爽狠狠视频| 福利成人午夜国产一区| 亚洲AV无码乱码在线观看性色扶| 97人妻免费碰视频碰免| 色综合AV综合无码综合网站| 国色天香中文字幕在线视频| 欧美寡妇xxxx黑人猛交| 精品久久免费国产乱色也| 亚洲熟妇自偷自拍另欧美| 久久久久亚洲A√无码| 亚洲一区中文字幕第十页| 久久精品国产免费观看频道| 国产精品精品一区二区三| 中文字幕av一区二区| 自拍偷在线精品自拍偷99| 免费又大粗又爽又黄少妇毛片| 国产免费无遮挡吃奶视频| 亚洲一区成人在线视频| 国产精品国产三级国av| 99久久精品国产熟女拳交| 四虎在线播放亚洲成人| 习水县| 久久精品视频一二三四区| 中文字幕精品人妻丝袜| 国产中文字幕在线一区|