如何:在 Outlook 中移动项
更新:2007 年 11 月
适用对象 |
---|
本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。 项目类型
Microsoft Office 版本
有关更多信息,请参见按应用程序和项目类型提供的功能。 |
此示例将未阅读的电子邮件从“收件箱”移动到名为 Test 的文件夹中。此示例仅移动在 Subject 字段中具有文字 Test 的电子邮件。
示例
Private Sub ThisApplication_NewMail() Handles Application.NewMail
Dim inBox As Outlook.MAPIFolder = Me.Application.ActiveExplorer() _
.Session.GetDefaultFolder(Outlook _
.OlDefaultFolders.olFolderInbox)
Dim items As Outlook.Items = inBox.Items
Dim moveMail As Outlook.MailItem = Nothing
items.Restrict("[UnRead] = true")
Dim destFolder As Outlook.MAPIFolder = inBox.Folders("Test")
Try
For Each eMail As Object In items
moveMail = TryCast(eMail, Outlook.MailItem)
If Not moveMail Is Nothing Then
If InStr(moveMail.Subject, "Test") > 0 Then
moveMail.Move(destFolder)
End If
End If
Next eMail
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.NewMail += new Microsoft.Office.Interop.Outlook.
ApplicationEvents_11_NewMailEventHandler
(ThisAddIn_NewMail);
}
private void ThisAddIn_NewMail()
{
Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)this.Application.
ActiveExplorer().Session.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items items = (Outlook.Items)inBox.Items;
Outlook.MailItem moveMail = null;
items.Restrict("[UnRead] = true");
Outlook.MAPIFolder destFolder = inBox.Folders["Test"];
foreach (object eMail in items)
{
try
{
moveMail = eMail as Outlook.MailItem;
if (moveMail != null)
{
string titleSubject = (string)moveMail.Subject;
if (titleSubject.IndexOf("Test") > 0)
{
moveMail.Move(destFolder);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
编译代码
此示例需要:
取名为 Test 的 Outlook 邮件文件夹。
在 Subject 字段中具有文字 Test 的电子邮件。