HOW TO:在 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 文字的電子郵件訊息。