[轉]創建一個 Microsoft Outlook 擴展插件


本文轉自:https://coyee.com/article/10625-how-to-create-an-add-in-for-microsoft-outlook

1.介紹

Visual Studio Tool for Office(VSTO)加載項是.NET Framework中提供的工具集,可讓我們在(版本2003及更高版本)中擴展和自定義Microsoft Office產品。

在本教程中,我們將使用Outlook 2013作為案例研究和Visual Studio 2015。

2.Outlook對象模型

這是當您想創建Outlook加載項時的起點,了解這些對象的含義很重要。

Outlook對象模型:

  • Application:它代表Outlook應用程序,是模型中最高級別的對象。該對象是到達模型的其他對象的起點。
  • Explorer:它表示一個窗口來顯示文件夾的內容,如電子郵件,消息,任務,約會等。
  • Inspector:它表示一個顯示項目的窗口,如電子郵件消息,任務,約會等。
  • MAPIFolder:它代表一個包含電子郵件,聯系人,約會等的文件夾。備注:此對象已過時,替代地,您應該使用MAPIFolder的Folder 對象實例。
  • MailItem:它代表一封電子郵件。
  • AppointmentItem:它代表會議,一次性約會,日程預約或日歷 文件夾中的會議。
  • TaskItem:它表示在指定時間范圍內執行的任務。
  • ContactItem:它表示Contact文件夾中的聯系人。
 

3. 如何開始

在下一節中,我們將從這種類型的應用開始講解。

3.1. 如何創建項目

  1. 啟動Visual Studio。
  2. 文件菜單 / 新建/ 項目。
  3. 在項目的模型面板中,打開Visual C#,Office/SharePoint,Office 加載項。
  4. 選擇Outlook 2013 和 2016 VSTO 加載項加入模型。
  5. 填寫項目名稱,然后單擊OK。

3.2. 項目布局

實際上,為Visual Studio生成的項目布局是非常直觀和簡單的。

主類是 ThisAddIn ,代碼如下所示:

public partial class ThisAddIn { private void ThisAddIn_Startup(object sender, System.EventArgs e) { } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { // Note: Outlook no longer raises this event. If you have code that // must run when Outlook shuts down, see // http://go.microsoft.com/fwlink/?LinkId=506785 } #region VSTO generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); } #endregion }
 

這是一個非常簡單的類。 ThisAddIn_Startup方法是應用程序的起始點。 在這個方法中,我們可以得到對象Application和模型的其他對象。 此外,我們可以執行我們的初始化過程,如配置和訪問數據庫。

當用戶關閉Outlook時執行ThisAddIn_Shutdown方法。 但重要的是,在Outlook當前版本中,由於性能問題,此方法沒有被調用。 但是,如果在Outlook關閉時需要執行某些代碼,則可以檢查此鏈接以獲取其他選項。

 

3.3. 應用程序對象和其他模型對象

接下來,我們將會看到如何獲得一些對象模型。因此,我們需要使用必要的命名空間:

using Outlook = Microsoft.Office.Interop.Outlook;

代碼如下所示:

private void ThisAddIn_Startup(object sender, System.EventArgs e) { // Get the Application object Outlook.Application application = this.Application; // Get the Inspector object Outlook.Inspectors inspectors = application.Inspectors; // Get the active Inspector object Outlook.Inspector activeInspector = application.ActiveInspector(); if (activeInspector != null) { // Get the title of the active item when the Outlook start. MessageBox.Show("Active inspector: " + activeInspector.Caption); } // Get the Explorer objects Outlook.Explorers explorers = application.Explorers; // Get the active Explorer object Outlook.Explorer activeExplorer = application.ActiveExplorer(); if (activeExplorer != null) { // Get the title of the active folder when the Outlook start. MessageBox.Show("Active explorer: " + activeExplorer.Caption); } }
 

其他對象模型可以通過使用 Inspector 和 Explorer 對象來獲取。該操作通常基於事件,因此,需要注冊這兩個對象創建的事件。代碼大概是這樣的:

private void ThisAddIn_Startup(object sender, System.EventArgs e) { // ... // Add a new Inspector to the application inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_AddTextToNewMail); }

Inspectors_AddTextToNewMail 是實現我們功能的方法,如下所示:

 
void Inspectors_AddTextToNewMail(Outlook.Inspector inspector) { }

inspector參數應當是對電子郵件或聯系人的引用,具體取決於Outlook中的用戶操作。

3.4.在項目結束

在項目結束時,要從開發計算機上的Outlook中刪除加載項,請轉到Visual Studio中的生成(Build )菜單,然后單擊清除解決方案(Clean Solution)選項。

3.5.如何制作安裝程序

在Visual Studio中,轉到Build菜單/“Publish ...”。

備注:有時Visual Studio生成的安裝程序可能會在安裝在用戶計算機中時失敗,您應該會收到以下錯誤消息:

 

引用:

“不能解析屬性為'類型'的值。錯誤:無法加載文件或程序集…“。

要解決這個錯誤,請查看此鏈接 和 此鏈接

4. 基本示例

在下一節中,我們將展示一些關於如何為Outlook 2013創建VSTO加載項的示例。

4.1. 如何處理新的電子郵件

下面的示例是,用戶創建一封新的電子郵件時,我們會在郵件的主題和正文中插入自定義文本。為了完成這個任務,我們需要在ThisAddIn_Startup方法中注冊一個新的 Inspector ,當用戶創建或者打開郵件的時候,就會觸發調用Inspectors_AddTextToNewMail方法。

 
private void ThisAddIn_Startup(object sender, System.EventArgs e) { // Get the Application object Outlook.Application application = this.Application; // Add a new Inspector inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_AddTextToNewMail); } void Inspectors_AddTextToNewMail(Outlook.Inspector inspector) { // Get the current item for this Inspecto object and check if is type // of MailItem Outlook.MailItem mailItem = inspector.CurrentItem as Outlook.MailItem; if (mailItem != null) { if (mailItem.EntryID == null) { mailItem.Subject = "My subject text"; mailItem.Body = "My body text"; } } }
 

注意: 需要檢查 mailItem 對象是否是一種 MailItem 類,因為當這個事件被觸發時,通過  Outlook 的用戶操作,我們並不知道哪個Inspector對象會被執行。 

4.2. 如何處理一封在發送的郵件

下面的示例是,在電子郵件發送時,允許我們更新郵件里的消息。這是一個非常有趣和適用的功能。有一些Outlook加載項可以執行這種類型的操作,例如,防病毒軟件需要在郵件被發送時包含簽名。

為了完成這些功能,我們將會在 ItemSend 事件中插入我們的代碼。當一個郵件通過用戶或者計划任務被發送時,這個事件就會被觸發。

 
private void ThisAddIn_Startup(object sender, System.EventArgs e) { // Get the Application object Outlook.Application application = this.Application; // Subscribe to the ItemSend event, that it's triggered when an email is sent application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler( ItemSend_BeforeSend); } void ItemSend_BeforeSend(object item, ref bool cancel) { Outlook.MailItem mailItem = (Outlook.MailItem) item; if (mailItem != null) { mailItem.Body += "Modified by GettingStartedOutlookAddIn"; } cancel = false; }
 

4.3.如何將控件添加到功能區工具欄

在下面的示例中,我們將看到如何在Outlook中的功能區(工具欄)中添加控件。 具體來說,當用戶編輯或讀取電子郵件時,如何添加按鈕到功能區。

執行以下操作:

  • 向項目添加一個新項目。 在本例中,我命名為RibbonDemo。

  • 在功能區設計器中,選擇功能區組件並轉到屬性窗口。
  • 查找RibbonType屬性並選擇Microsoft.Outlook.Mail.Compose和Microsoft.Outlook.Mail.Read的值,它指的是創建和讀取電子郵件的功能區。
  • 我們為組設置一個合適的名稱。 為了完成它,選擇它並在屬性窗口中查找Label屬性並為其鍵入名稱。

  • 接下來,我們從ToolBox中添加按鈕並准備好。
  • 或者,使用按鈕的ControlSize和ShowImage屬性,我們可以完成Microsoft Office產品的外觀。​​​​​​​

 

接下來就像開發 C# 桌面應用程序一樣,讓我們編寫 ButtonDemo按鈕的OnClick事件來處理郵件消息。

private void buttonDemo_Click(object sender, RibbonControlEventArgs e) { // Get the Application object Outlook.Application application = Globals.ThisAddIn.Application; // Get the active Inspector object and check if is type of MailItem Outlook.Inspector inspector = application.ActiveInspector(); Outlook.MailItem mailItem = inspector.CurrentItem as Outlook.MailItem; if (mailItem != null) { MessageBox.Show("Subject: " + mailItem.Subject); } }
 

5.其他對象模型的例子

在下面的部分中,我們將看到需要訪問其他對象模型的示例。

將我們的內容放置在上下文中,當我們使用電子郵件正文時,我們可以使用Outlook對象執行一些功能,例如訪問MailItem對象的Body屬性。 但是,如果需要更多地控制電子郵件正文,則需要將其作為Word文檔。 接下來,我們將會看到一些例子。

開始之前

首先,我們將看到如何從“Outlook 2013和2016 VSTO加載項”中引用Word對象模型。 選擇您在Outlook應用程序中使用的相同版本以及選擇工具和實用程序的引用非常重要。

 

在我的案例中,我使用的是15.0.0.0 版本的Outlook。

因此,我們將選擇相同版本的 Word 引用。

5.1. 如何通過 Ribbon 上的按鈕獲取電子郵件里選中的文本。

下面的示例是,一個含OnClick 事件的按鈕添加到Ribbon(參見前面的章節 4.3. 如何添加一個控件到 Ribbon 工具欄)。

private void button2Demo_Click(object sender, RibbonControlEventArgs e) { // Get the Application object Outlook.Application application = Globals.ThisAddIn.Application; // Get the active Inspector object and check if is type of MailItem Outlook.Inspector inspector = application.ActiveInspector(); Outlook.MailItem mailItem = inspector.CurrentItem as Outlook.MailItem; if (mailItem != null) { Word.Document document = (Word.Document) inspector.WordEditor; string selectedText = document.Application.Selection.Text; MessageBox.Show(selectedText); } }
 

當用戶創建新的電子郵件時,會出現以下圖片。 在這里,我們可以看到應用程序顯示一條消息,其中包含用戶已選擇並單擊“獲取文本選擇”按鈕的文本。

5.2. 如何訂閱電子郵件正文的事件

在下面的示例中,我們將演示如何將我們的應用程序訂閱到用戶通過電子郵件正文執行的事件。 具體來說,我們將訂閱Word文檔中的事件(它代表電子郵件正文),當用戶對文本進行雙擊時,我們將獲得點擊完成時的單詞。

 

我們將從應用程序入口點開始,我們將創建一個Inspector對象來監視用戶何時創建/編輯電子郵件。

private void ThisAddIn_Startup(object sender, System.EventArgs e) { // Get the Application object Outlook.Application application = this.Application; // Add a new Inspector inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler( Inspectors_RegisterEventWordDocument); }

使用這個Inspector,我們在打開電子郵件編輯器時執行我們的代碼。 此時,我們將檢查Inspector對象是否為MailItem。 接下來,我們將檢查電子郵件編輯器是否是Word編輯器(有時是另一種類型),最后我們將獲取Word文檔對象。

 
void Inspectors_RegisterEventWordDocument(Outlook.Inspector inspector)
{
    Outlook.MailItem mailItem = inspector.CurrentItem as Outlook.MailItem; if (mailItem != null) { // Check that the email editor is Word editor // Although "always" is a Word editor in Outlook 2013, it's best done perform this check if (inspector.EditorType == Outlook.OlEditorType.olEditorWord && inspector.IsWordMail()) { // Get the Word document Word.Document document = inspector.WordEditor; if (document != null) { // Subscribe to the BeforeDoubleClick event of the Word document document.Application.WindowBeforeDoubleClick += new Word.ApplicationEvents4_WindowBeforeDoubleClickEventHandler( ApplicationOnWindowBeforeDoubleClick); } } } }
 

接下來,我們將訂閱我們得到的Word文檔的BeforeDoubleClick事件(在以前的代碼中),當觸發事件時,我們將選擇用戶點擊的單詞。

private void ApplicationOnWindowBeforeDoubleClick(Word.Selection selection, ref bool cancel) { // Get the selected word Word.Words words = selection.Words; MessageBox.Show("Selection: " + words.First.Text); }

我們可以通過“選擇(selection)”對象訪問用戶選擇的單詞,它具有很多功能。 在我們的示例中,使用Word屬性,我們得到用戶所有選定單詞的集合,第一個屬性是用戶點擊的位置。

 

當用戶創建新的電子郵件時,會出現以下圖片。 在這里,我們可以看到應用程序顯示一個消息,其中包含用戶雙擊的文本。

6.結論

可以看出,使用VSTO加載項工具,我們可以通過多種方式輕松擴展Outlook功能。 在我看來,企業部門的這種應用有很多要求,可以快速解決某些問題,比起其他應用程序,通常辦公室用戶對微軟Office產品感到更滿意。 使用VSTO加載項,我們可以查詢數據庫以獲取員工的聯系人,將產品列表包含在電子郵件中,同步企業應用程序與Outlook之間的約會等等。

7.參考文獻


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM