从Outlook中的特定文件夹导入电子邮件

时间:2017-04-07 08:35:38

标签: excel vba excel-vba outlook outlook-vba

我目前在Excel中使用以下代码来访问除我自己以外的无人版Outlook邮箱中的文件夹。

但是有一种方法可以在代码中设置文件夹,而不是使用文件夹选择器。

Sub Launch_Pad()   
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder

Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.PickFolder

n = 2
Cells.ClearContents

Call ProcessFolder(olFolder)

Set olNS = Nothing
Set olFolder = Nothing
Set olApp = Nothing
Set olNS = Nothing
End Sub

Sub ProcessFolder(olfdStart As Outlook.MAPIFolder)

Dim olFolder As Outlook.MAPIFolder
Dim olObject As Object
Dim olMail As Outlook.MailItem
   n = 1
For Each olObject In olfdStart.Items
    If TypeName(olObject) = "MailItem" Then

        n = n + 1
        Set olMail = olObject
            Cells(n, 1) = olMail.Subject
            Cells(n, 2) = olMail.ReceivedTime
            Cells(n, 3) = olMail.Body

    End If
Next
Set olMail = Nothing
Set olFolder = Nothing
Set olObject = Nothing
End Sub

3 个答案:

答案 0 :(得分:2)

感谢Erdem

Sub ShareMail()

    Dim olNamespace As Outlook.Namespace
    Dim olApp As Outlook.Application
    Dim olNs As Outlook.Namespace
    Dim olFolder As Outlook.MAPIFolder
    Dim olRecip As Outlook.Recipient

    Set olApp = Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set olRecip = olNs.CreateRecipient("mail@mail.com")
    Set olFolder = olNs.GetSharedDefaultFolder(olRecip, olFolderInbox).Folders("myfolder")

   Call ProcessFolder(olFolder)

    Set olApp = Nothing
    Set olNs = Nothing
    Set olRecip = Nothing
    Set olFolder = Nothing


End Sub

答案 1 :(得分:1)

如果文件夹应该是收件箱,您可以在下面使用

Set olFolder = olNS.GetDefaultFolder(olFolderInbox)

或者是子文件夹

Set olFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("mysubfolder")

答案 2 :(得分:1)

使用 GetSharedDefaultFolder Method 来访问其他用户的一个或多个默认文件夹。

这里的示例是共享收件箱

Dim olApp As Outlook.Application
Dim olNs As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim olRecip As Outlook.Recipient

Set olApp = Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set olRecip = olNs.CreateRecipient("0m3r@EmailAddress.com")
Set olFolder = olNs.GetSharedDefaultFolder(olRecip, olFolderInbox)

Or

Set olFolder = olNs.GetSharedDefaultFolder(olRecip, olFolderInbox).Folders("SubfolderName")
  

GetSharedDefaultFolder Method返回一个MAPIFolder对象,表示指定用户的指定默认文件夹。此方法用于委派方案,其中一个用户已为一个或多个默认文件夹(例如,其共享的日历文件夹)委派了另一个用户的访问权限。