下面给出完整的代码:
OutlookToWord
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 using System.Windows.Forms;
9 using Word = Microsoft.Office.Interop.Word;
10 using System.IO;
11
12 namespace OutlookToWord
13 {
14 public partial class ThisAddIn
15 {
16 private Office.CommandBar newToolBar;
17 private Office.CommandBarButton exportButton;
18 private Outlook.Explorers selectExplorers;
19 private FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
20
21 private void ThisAddIn_Startup(object sender, System.EventArgs e)
22 {
23 selectExplorers = this.Application.Explorers;
24 selectExplorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(newExplorer_Event);
25 AddToolbar();
26 }
27
28 private void newExplorer_Event(Outlook.Explorer new_Explorer)
29 {
30 ((Outlook._Explorer)new_Explorer).Activate();
31 newToolBar = null;
32 AddToolbar();
33 }
34
35 private void AddToolbar()
36 {
37 if (newToolBar == null)
38 {
39 Office.CommandBars cmdBars = this.Application.ActiveExplorer().CommandBars;
40 newToolBar = cmdBars.Add("NewToolBar", Office.MsoBarPosition.msoBarTop, false, true);
41 }
42 try
43 {
44 Office.CommandBarButton btExportToWord = (Office.CommandBarButton)newToolBar.Controls.Add(1, missing, missing, missing, missing);
45 btExportToWord.Style = Office.MsoButtonStyle.msoButtonCaption;
46 btExportToWord.Caption = "Export to Word";
47 btExportToWord.Tag = "Export current mail to word";
48 if (this.exportButton == null)
49 {
50 this.exportButton = btExportToWord;
51 exportButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(exportButton_Click);
52 }
53 newToolBar.Visible = true;
54 }
55 catch (Exception ex)
56 {
57 MessageBox.Show(ex.Message);
58 }
59 }
60
61 /// <summary>
62 /// Create export file name from a string.
63 /// </summary>
64 /// <param name="sPath"></param>
65 /// <param name="sFileName"></param>
66 /// <returns></returns>
67 private string CreateFileName(string sPath, string sFileName)
68 {
69 // Remove unsupport charts for file name.
70 string sRst = sFileName.Replace("\\", "");
71 sRst = sRst.Replace("/", "");
72 sRst = sRst.Replace(":", "");
73 sRst = sRst.Replace("*", "");
74 sRst = sRst.Replace("?", "");
75 sRst = sRst.Replace("\"\"", "");
76 sRst = sRst.Replace("<", "");
77 sRst = sRst.Replace(">", "");
78 sRst = sRst.Replace("|", "");
79
80 if (sRst.Length > 100)
81 {
82 sRst = sRst.Substring(0, 100);
83 }
84
85 // Make sure the file name is unique.
86 int i = 1;
87 if (File.Exists(string.Concat(sPath, sRst, ".docx")))
88 {
89 while (true)
90 {
91 if (File.Exists(string.Concat(sPath, sRst, i.ToString(), ".docx")))
92 {
93 i++;
94 }
95 else
96 {
97 sRst += i.ToString();
98 break;
99 }
100 }
101 }
102
103 // Return *.docx file name.
104 return string.Concat(sPath, sRst, ".docx");
105 }
106
107 void exportButton_Click(Office.CommandBarButton ctrl, ref bool cancel)
108 {
109 object sFileName;
110 string sPath = string.Empty;
111
112 // Get mail export path.
113 if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
114 {
115 sPath = folderBrowserDialog.SelectedPath;
116
117 // If the selected path is a folder, add '\' at the end of the path string.
118 if (sPath != Path.GetPathRoot(sPath))
119 {
120 sPath += "\\";
121 }
122 }
123 else
124 {
125 return;
126 }
127
128 Word.Application app = new Word.Application();
129 Word.Document doc = null;
130 object unknow = Type.Missing;
131 object format = Word.WdSaveFormat.wdFormatDocumentDefault;
132 Outlook.Explorer activeExplorer = this.Application.Explorers.Application.ActiveExplorer();
133
134 try
135 {
136 // Export all selected mail items to word.
137 foreach (object selectedItem in activeExplorer.Selection)
138 {
139 Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;
140 if (mailItem != null)
141 {
142 sFileName = CreateFileName(sPath, mailItem.Subject);
143 //doc = app.Documents.Add(ref unknow, ref unknow, ref unknow, ref unknow);
144 //doc.Content.Text = mailItem.Body;
145 //doc.Paragraphs.Last.Range.Text = mailItem.Body;
146
147 Outlook.Inspector inspector = mailItem.GetInspector;
148 doc = (Word.Document)inspector.WordEditor;
149 //mailItem.SaveAs(sFileName.ToString(), Outlook.OlSaveAsType.olDoc);
150
151 doc.SaveAs(ref sFileName, ref format, ref unknow, ref unknow, ref unknow,
152 ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
153 ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
154 //doc.Close(ref unknow, ref unknow, ref unknow);
155 }
156 }
157 }
158 catch (Exception ex)
159 {
160 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
161 }
162 finally
163 {
164 if (app != null)
165 {
166 app.Quit(ref unknow, ref unknow, ref unknow);
167 }
168 }
169 }
170
171 private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
172 {
173 }
174
175 #region VSTO generated code
176
177 /// <summary>
178 /// Required method for Designer support - do not modify
179 /// the contents of this method with the code editor.
180 /// </summary>
181 private void InternalStartup()
182 {
183 this.Startup += new System.EventHandler(ThisAddIn_Startup);
184 this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
185 }
186
187 #endregion
188 }
189 }
本文导航
- 第1页: 首页
- 第2页: 导出功能的核心代码
- 第3页: 完整的代码