西西軟件園多重安全檢測下載網(wǎng)站、值得信賴的軟件下載站!
軟件
軟件
文章
搜索

首頁編程開發(fā)C#.NET → VS2010將Outlook郵件導(dǎo)出成Word文檔格式

VS2010將Outlook郵件導(dǎo)出成Word文檔格式

相關(guān)軟件相關(guān)文章發(fā)表評論 來源:本站整理時間:2011/1/28 9:08:52字體大小:A-A+

作者:佚名點擊:1772次評論:1次標(biāo)簽: outlook word vs2010

  • 類型:郵箱工具大。261KB語言:中文 評分:10.0
  • 標(biāo)簽:
立即下載
3 頁 完整的代碼


  下面給出完整的代碼:
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 }

    相關(guān)評論

    閱讀本文后您有什么感想? 已有人給出評價!

    • 8 喜歡喜歡
    • 3 頂
    • 1 難過難過
    • 5 囧
    • 3 圍觀圍觀
    • 2 無聊無聊

    熱門評論

    最新評論

    發(fā)表評論 查看所有評論(1)

    昵稱:
    表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
    字?jǐn)?shù): 0/500 (您的評論需要經(jīng)過審核才能顯示)