• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

VS2010将Outlook邮件导出成Word文档格式

时间:2015年04月02日 15:37:27    来源:魔法猪系统重装大师官网    人气:11693

 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,这里有非常详细的介绍,将代码拷到工程中,并做适当的修改。

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 ///


68 /// Required method for Designer support - do not modify
69 /// the contents of this method with the code editor.
70 ///

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工具栏中创建了一个Export to Word按钮。退出Outlook在Visual Studio中直接按F5运行查看效果。


  如果你的Outlook工具栏中没有Add-ins菜单,请点击File-Options-Customize Ribbon,在其中勾选Add-Ins来显示它。



  接下来的任务就是实现邮件的导出功能了。由于需要在程序中调用Word的部分功能,所以你需要在工程添加Word的相关引用,如下图。注意你本地可能包含多个Microsoft.Office.Interop.Word程序集的版本,选择最高版本的那个即可。


  然后在代码中加入Word程序集的命名空间using Word = Microsoft.Office.Interop.Word。我们希望在导出邮件之前提示用户选择要导出文件的位置,因此程序中还需要一个文件夹选择对话框控件,在程序中给出定义。现在我们来看看导出功能的核心代码。

 

123在本页阅读全文
本文导航
  • 第1页: 首页
  • 第2页: 导出功能的核心代码
  • 第3页: 完整的代码
VS2010,将,Outlook,邮件,导出,成,Word,
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部