共用方式為


HOW TO:在 Outlook 中移動項目

這個範例會從 [收件匣] 將未讀取的電子郵件訊息移至名為 Test 的資料夾中。 此範例只會移動 Subject 欄位中含有 Test 文字的訊息。

**適用於:**本主題中的資訊適用於 Outlook 2007 和 Outlook 2010 的應用程式層級專案。如需詳細資訊,請參閱依 Office 應用程式和專案類型提供的功能

範例

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
        MsgBox(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 文字的電子郵件訊息。

請參閱

工作

HOW TO:依名稱擷取資料夾

HOW TO:在特定資料夾中搜尋

HOW TO:在收到電子郵件訊息時執行動作

概念

使用資料夾