本文介绍了如何在Outlook中获取特定文件夹的MailItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件夹在Outlook中称为 "Request Mailbox"

如何获取该文件夹中所有 mailitems 的列表

解决方案

要获取 MailItems 的列表,只需执行此操作

MailItem列表将显示为电子邮件

Option Explicit
Sub MailItems()
    Dim olNamespace As Outlook.NameSpace
    Dim olFolder  As Outlook.MAPIFolder
    Dim olItem As Outlook.MailItem

    Set olNamespace = Application.GetNamespace("MAPI")
    Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox).Folders("Request Mailbox")

    Set olItem = Application.CreateItem(olMailItem) ' Creat EMail
    With olItem
        For Each olItem In olFolder.Items
            Debug.Print olItem.Subject ' Print to immediate window
            .body = .body & olItem.Subject & vbCrLf ' Print to Email
            Debug.Print olItem.SenderName
            .body = .body & olItem.SenderName & vbCrLf
            Debug.Print olItem.ReceivedTime
            .body = .body & olItem.ReceivedTime & vbCrLf & vbCrLf
        Next ' vbCrLf = vb: Visual Basic Cr: Carriage Return Lf: LineFeed
        .Subject = "Mail Items" ' Subject
        .Display    ' Display Msg
    End With

End Sub

对于共享文件夹,请尝试此

Option Explicit
Sub ShareMailItems()
    Dim olNamespace As Outlook.NameSpace
    Dim olShareName As Outlook.Recipient
    Dim olShareInbox As Outlook.Folder
    Dim olItem As Outlook.MailItem

    Set olNamespace = Application.GetNamespace("MAPI")
    Set olShareName = olNamespace.CreateRecipient("[email protected]") '// Owner's email address
    Set olShareInbox = olNamespace.GetSharedDefaultFolder( _
                 olShareName, olFolderInbox).Folders("Request Mailbox") '// FolderName

    Set olItem = Application.CreateItem(olMailItem) ' Creat EMail
    With olItem
        For Each olItem In olShareInbox.Items
            Debug.Print olItem.Subject ' Print to immediate window
            .body = .body & olItem.Subject & vbCrLf ' Print to Email
            Debug.Print olItem.SenderName
            .body = .body & olItem.SenderName & vbCrLf
            Debug.Print olItem.ReceivedTime
            .body = .body & olItem.ReceivedTime & vbCrLf & vbCrLf
        Next ' vbCrLf = vb: Visual Basic Cr: Carriage Return Lf: LineFeed
        .Subject = "Mail Items" ' Subject
        .Display    ' Display
    End With
End Sub

My folder is called "Request Mailbox" in Outlook

How can I get a list of all mailitems in that folder

解决方案

To get list of MailItems, you can simply do this

MailItem list will be displayed as Email

Option Explicit
Sub MailItems()
    Dim olNamespace As Outlook.NameSpace
    Dim olFolder  As Outlook.MAPIFolder
    Dim olItem As Outlook.MailItem

    Set olNamespace = Application.GetNamespace("MAPI")
    Set olFolder = olNamespace.GetDefaultFolder(olFolderInbox).Folders("Request Mailbox")

    Set olItem = Application.CreateItem(olMailItem) ' Creat EMail
    With olItem
        For Each olItem In olFolder.Items
            Debug.Print olItem.Subject ' Print to immediate window
            .body = .body & olItem.Subject & vbCrLf ' Print to Email
            Debug.Print olItem.SenderName
            .body = .body & olItem.SenderName & vbCrLf
            Debug.Print olItem.ReceivedTime
            .body = .body & olItem.ReceivedTime & vbCrLf & vbCrLf
        Next ' vbCrLf = vb: Visual Basic Cr: Carriage Return Lf: LineFeed
        .Subject = "Mail Items" ' Subject
        .Display    ' Display Msg
    End With

End Sub

For Shared Folder Try this

Option Explicit
Sub ShareMailItems()
    Dim olNamespace As Outlook.NameSpace
    Dim olShareName As Outlook.Recipient
    Dim olShareInbox As Outlook.Folder
    Dim olItem As Outlook.MailItem

    Set olNamespace = Application.GetNamespace("MAPI")
    Set olShareName = olNamespace.CreateRecipient("[email protected]") '// Owner's email address
    Set olShareInbox = olNamespace.GetSharedDefaultFolder( _
                 olShareName, olFolderInbox).Folders("Request Mailbox") '// FolderName

    Set olItem = Application.CreateItem(olMailItem) ' Creat EMail
    With olItem
        For Each olItem In olShareInbox.Items
            Debug.Print olItem.Subject ' Print to immediate window
            .body = .body & olItem.Subject & vbCrLf ' Print to Email
            Debug.Print olItem.SenderName
            .body = .body & olItem.SenderName & vbCrLf
            Debug.Print olItem.ReceivedTime
            .body = .body & olItem.ReceivedTime & vbCrLf & vbCrLf
        Next ' vbCrLf = vb: Visual Basic Cr: Carriage Return Lf: LineFeed
        .Subject = "Mail Items" ' Subject
        .Display    ' Display
    End With
End Sub

这篇关于如何在Outlook中获取特定文件夹的MailItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-22 03:59