VS2010将Outlook邮件导出成Word⽂档格式
周末加班,整理⼀下outlook中的邮件,其中有个⼏个邮件⽬录中的⽂件都是重要的资料,想要导出成word,于是按照我的思维惯性,⾸先⼿⼯实现了⼀次,⼤概⼯序如下:
1、⾸先打开邮件,ctrl+A全选邮件;
2、然后在备份⽬录下新建⼀个word⽂档,黏贴进去;
3、重命名word⽂件;
⼿⼯操作了⼏封邮件之后,发现⼈⾁是有点累,看到数以万封的邮件等着导出,顿感绝望,于是再次按照我的思惟惯性,需要事先⼀个⾃动化的程序来实现。
Visual Studio允许创建Office类型的⼯程,⽤于给Office产品创建外接程序以及⾃定义模板等。这是⼀个⾮常实⽤的功能,在早期版本的Office中我们只能通过VBA代码来实现⼀些⾃定义的功能,如⽂本的⾃动替换、宏录制功能等等。VBA的功能很有限,有些时候我们希望⾃定义程序能够完成更多的功能,⽐如在Office多个不同产品之间进⾏⽂档转换、调⽤系统API、远程过程调⽤及Web Service访问等。下⾯是在Visual Studio 2010中创建Office类型⼯程的对话框。
本例中我将向⼤家介绍如何通过Office外接程序将Outlook中的邮件导出到本地Word⽂档中。当然,你完全可以⼿动将Outlook中的邮件通过复制粘贴的⽅式拷贝到Word⽂档中,但是要想同时将⼤批的邮件导出到Word中恐怕就需要借助于程序来完成了。来看看我们如何实现这个⼩插件!
⾸先在Visual Studio中创建Outlook 2010 Add-in类型的⼯程,取名为OutlookToWord。这⾥需要申明⼀下我开发和测试的环境:Visual Studio 2010 + Office 2010。当然,在Visual Studio 2008和稍低版本的Office中同样也可以实现,只是⼯程类型和外接程序所⽀持的载体版本稍有区别。
⼯程创建成功后Visual Studio会⾃动为你⽣成⼀些⽂件和代码,我们只需要在代码中实现⾃定义的功能即可。我们希望程序通过⼀个按钮来实现邮件的导出,因此需要在Outlook的⼯具栏中创建⼀个⾃定义按钮。这个很简单,直接查MSDN,这⾥有⾮常详细的介绍,将代码拷到⼯程中,并做适当的修改。
完整的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using System.IO;
namespace OutlookToWord
{
public partial class ThisAddIn
{
private Office.CommandBar newToolBar;今年五一放假几天从几号到几号
private Office.CommandBarButton exportButton;
private Outlook.Explorers selectExplorers;
private FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
selectExplorers = this.Application.Explorers;
selectExplorers.NewExplorer += new Outlook
.ExplorersEvents_NewExplorerEventHandler(newExplorer_Event);
AddToolbar();
}
private void newExplorer_Event(Outlook.Explorer new_Explorer)
{
((Outlook._Explorer)new_Explorer).Activate();
newToolBar = null;
AddToolbar();
}
private void AddToolbar()
{
if (newToolBar == null)
{
Office.CommandBars cmdBars = this.Application.ActiveExplorer().CommandBars;
newToolBar = cmdBars.Add("NewToolBar", Office.MsoBarPosition.msoBarTop, false, true);
}
try
{
Office.CommandBarButton btExportToWord = (Office.CommandBarButton)newToolBar.Controls.Add(1, missing, missing, missing, missing); //btExportToWord.Style = Office.MsoButtonStyle.msoButtonCaption;
btExportToWord.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
btExportToWord.Caption = "导出word";
btExportToWord.Tag = "Export current mail to word";
btExportToWord.Picture = getImage();
if (portButton == null)
{
exportButton.Click += new Office._CommandBarButtonEvents_ClickEventHandler(exportButton_Click);
}
newToolBar.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// Create export file name from a string.
/// </summary>
/
// <param name="sPath"></param>
/// <param name="sFileName"></param>
/// <param name="sFileName"></param>
/// <returns></returns>
private string CreateFileName(string sPath, string sFileName)
{
// Remove unsupport charts for file name.
string sRst = sFileName.Replace("\\", "");
sRst = sRst.Replace("/", "");
sRst = sRst.Replace(":", "");
sRst = sRst.Replace("*", "");
sRst = sRst.Replace("?", "");
sRst = sRst.Replace("\"\"", "");
sRst = sRst.Replace("<", "");
sRst = sRst.Replace(">", "");
sRst = sRst.Replace("|", "");
if (sRst.Length > 100)
{
sRst = sRst.Substring(0, 100);
}
马苏资料// Make sure the file name is unique.
int i = 1;
if (File.Exists(string.Concat(sPath, sRst, ".docx")))
{
英语话剧while (true)
{
if (File.Exists(string.Concat(sPath, sRst, i.ToString(), ".docx")))
{
i++;
}
else
{
sRst += i.ToString();
break;
}
}
}
// Return *.docx file name.
return string.Concat(sPath, sRst, ".docx");
}
private void exportButton_Click(Office.CommandBarButton ctrl, ref bool cancel) {
//MessageBox.Show("You clicked: " + ctrl.Caption);
object sFileName;
string sPath = string.Empty;
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
sPath = folderBrowserDialog.SelectedPath;
if (sPath != Path.GetPathRoot(sPath))
{
sPath += "\\";
}
}
else
{
return;
}
Word.Application app = new Word.Application();
Word.Application app = new Word.Application();
Word.Document doc = null;
object unknow = Type.Missing;
object format = Word.WdSaveFormat.wdFormatDocumentDefault;
Outlook.Explorer activeExplorer = this.Application.Explorers.Application.ActiveExplorer();
try
{
// Export all selected mail items to word.
foreach (object selectedItem in activeExplorer.Selection)
候旭
{
Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;
if (mailItem != null)
{
sFileName = CreateFileName(sPath, mailItem.Subject);
Outlook.Inspector inspector = mailItem.GetInspector;
doc = (Word.Document)inspector.WordEditor;
doc.SaveAs(ref sFileName, ref format, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
//附件处理⽅案有两个;
/
/1、直接另存附件(⽐较原始,当前⽅案);
//2、附件插⼊邮件word正⽂之后(优化⽅案,未实现);
if (mailItem.Attachments.Count > 0)
{
for (int i = 1; i <= mailItem.Attachments.Count; i++)
{
mailItem.Attachments[i].SaveAsFile
(sPath + //@"C:\TestFileSave\" +
mailItem.Attachments[i].FileName);
}
}
/
/doc.Close(ref unknow, ref unknow, ref unknow);
}
}
}catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }finally
{
if (app != null)
{
app.Quit(ref unknow, ref unknow, ref unknow);
}
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)在淘宝网上如何开店
{
王宝强和女友冯清同框现身}
private stdole.IPictureDisp getImage()
{
{
stdole.IPictureDisp tempImage = null;
try
{
System.Drawing.Icon newIcon =
Properties.Resources.bigicon_48;
ImageList newImageList = new ImageList();
newImageList.Images.Add(newIcon);
tempImage = ConvertImage.Convert(newImageList.Images[0]); }
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return tempImage;
}
#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
}
sealed public class ConvertImage : System.Windows.Forms.AxHost
{
private ConvertImage()
: base(null)
{
}
public static stdole.IPictureDisp Convert
(System.Drawing.Image image)
{
return (stdole.IPictureDisp)System.
Windows.Forms.AxHost
.
GetIPictureDispFromPicture(image);
}
}
}
运⾏效果如下:
发布评论