从Outlook中的特定文件夹导入电子邮件
我目前在Excel中使用下面的代码来访问非我自己的无人Outlook邮箱中的文件夹。
但是有没有一种方法可以在代码中设置文件夹,而不是使用文件夹select器。
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
谢谢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
如果该文件夹应该是收件箱,您可以在下面使用
Set olFolder = olNS.GetDefaultFolder(olFolderInbox)
或者为一个子文件夹
Set olFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("mysubfolder")
使用GetSharedDefaultFolder方法访问另一个用户的一个或多个默认文件夹。
这里的示例是共享收件箱
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方法返回一个
MAPIFolder
对象,该对象表示指定用户的指定默认文件夹。 在委派scheme中使用此方法,其中一个用户已委派他人的一个或多个默认文件夹(例如,他们的共享日历文件夹)访问另一个用户。