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

问题描述:

我目前在Excel中使用下面的代码来访问非我自己的无人Outlook邮箱中的文件夹。从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 

如果文件夹应该是您可以使用下面

Set olFolder = olNS.GetDefaultFolder(olFolderInbox) 

或者一个子文件夹的收件箱

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

谢谢,但在自己的收件箱文件夹,而不是沿着边我自己 –

+1

尝试'设置olFolder = olNs.GetSharedDefaultFolder(olRecip,olFolderInbox列出的其他邮箱这只作品)。文件夹(“mysubfolder”)'? –

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("[email protected]") 
Set olFolder = olNs.GetSharedDefaultFolder(olRecip, olFolderInbox) 

Or 

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

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

+0

Thx,我可以访问收件箱,但我如何访问子文件夹 –

+0

@kdawg请记住https://meta.stackexchange.com/a/5235/289619 – 0m3r

感谢埃德姆

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("[email protected]") 
    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