Visual Studio允許創(chuàng)建Office類型的工程,用于給Office產品創(chuàng)建外接程序以及自定義模板等。這是一個非常實用的功能,在早期版本的Office中我們只能通過VBA代碼來實現(xiàn)一些自定義的功能,如文本的自動替換、宏錄制功能等等。VBA的功能很有限,有些時候我們希望自定義程序能夠完成更多的功能,比如在Office多個不同產品之間進行文檔轉換、調用系統(tǒng)API、遠程過程調用及Web Service訪問等。下面是在Visual Studio 2010中創(chuàng)建Office類型工程的對話框。
本例中我將向大家介紹如何通過Office外接程序將Outlook中的郵件導出到本地Word文檔中。當然,你完全可以手動將Outlook中的郵件通過復制粘貼的方式拷貝到Word文檔中,但是要想同時將大批的郵件導出到Word中恐怕就需要借助于程序來完成了。來看看我們如何實現(xiàn)這個小插件!
首先在Visual Studio中創(chuàng)建Outlook 2010 Add-in類型的工程,取名為OutlookToWord。這里需要申明一下我開發(fā)和測試的環(huán)境:Visual Studio 2010 + Office 2010。當然,在Visual Studio 2008和稍低版本的Office中同樣也可以實現(xiàn),只是工程類型和外接程序所支持的載體版本稍有區(qū)別。
工程創(chuàng)建成功后Visual Studio會自動為你生成一些文件和代碼,我們只需要在代碼中實現(xiàn)自定義的功能即可。我們希望程序通過一個按鈕來實現(xiàn)郵件的導出,因此需要在Outlook的工具欄中創(chuàng)建一個自定義按鈕。這個很簡單,直接查MSDN,這里有非常詳細的介紹,將代碼拷到工程中,并做適當?shù)男薷摹?br />
http://msdn.microsoft.com/zh-CN/library/scff9c7c.aspx
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Xml.Linq;
6 using Outlook = Microsoft.Office.Interop.Outlook;
7 using Office = Microsoft.Office.Core;
8
9 namespace OutlookToWord
10 {
11 public partial class ThisAddIn
12 {
13 private Office.CommandBar newToolBar;
14 private Office.CommandBarButton exportButton;
15 private Outlook.Explorers selectExplorers;
16
17 private void ThisAddIn_Startup(object sender, System.EventArgs e)
18 {
19 selectExplorers = this.Application.Explorers;
20 selectExplorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(newExplorer_Event);
21 AddToolbar();
22 }
23
24 private void newExplorer_Event(Outlook.Explorer new_Explorer)
25 {
26 ((Outlook._Explorer)new_Explorer).Activate();
27 newToolBar = null;
28 AddToolbar();
29 }
30
31 private void AddToolbar()
32 {
33 if (newToolBar == null)
34 {
35 Office.CommandBars cmdBars = this.Application.ActiveExplorer().CommandBars;
36 newToolBar = cmdBars.Add("NewToolBar", Office.MsoBarPosition.msoBarTop, false, true);
37 }
38 try
39 {
40 Office.CommandBarButton btExportToWord = (Office.CommandBarButton)newToolBar.Controls.Add(1, missing, missing, missing, missing);
41 btExportToWord.Style = Office.MsoButtonStyle.msoButtonCaption;
42 btExportToWord.Caption = "Export to Word";
43 btExportToWord.Tag = "Export current mail to word";
44 if (this.exportButton == null)
45 {
46 this.exportButton = btExportToWord;
47 exportButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(exportButton_Click);
48 }
49 newToolBar.Visible = true;
50 }
51 catch (Exception ex)
52 {
53 MessageBox.Show(ex.Message);
54 }
55 }
56
57 void exportButton_Click(Office.CommandBarButton ctrl, ref bool cancel)
58 {
59 }
60
61 private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
62 {
63 }
64
65 #region VSTO generated code
66
67 /// <summary>
68 /// Required method for Designer support - do not modify
69 /// the contents of this method with the code editor.
70 /// </summary>
71 private void InternalStartup()
72 {
73 this.Startup += new System.EventHandler(ThisAddIn_Startup);
74 this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
75 }
76
77 #endregion
78 }
79 }
我們在Outlook工具欄中創(chuàng)建了一個Export to Word按鈕。退出Outlook在Visual Studio中直接按F5運行查看效果。
如果你的Outlook工具欄中沒有Add-ins菜單,請點擊File-Options-Customize Ribbon,在其中勾選Add-Ins來顯示它。
接下來的任務就是實現(xiàn)郵件的導出功能了。由于需要在程序中調用Word的部分功能,所以你需要在工程添加Word的相關引用,如下圖。注意你本地可能包含多個Microsoft.Office.Interop.Word程序集的版本,選擇最高版本的那個即可。
然后在代碼中加入Word程序集的命名空間using Word = Microsoft.Office.Interop.Word。我們希望在導出郵件之前提示用戶選擇要導出文件的位置,因此程序中還需要一個文件夾選擇對話框控件,在程序中給出定義,F(xiàn)在我們來看看導出功能的核心代碼。
本文導航
- 第1頁: 首頁
- 第2頁: 導出功能的核心代碼
- 第3頁: 完整的代碼