使用visual basic访问收件箱中的子文件夹?

时间:2011-11-30 07:21:50

标签: vba outlook outlook-vba subdirectory

Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items

我使用上面的代码访问主要的Outlook收件箱,但是如何使用vba访问收件箱中的文件夹和邮件!

3 个答案:

答案 0 :(得分:17)

非常接近:)

要获取收件箱下名为“temp”的文件夹中的所有邮件,请尝试使用

Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim olFolder As Outlook.MAPIFolder
Dim msg As Outlook.MailItem

Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("Temp")

For Each msg In olFolder.Items
    Debug.Print msg.Subject
Next

答案 1 :(得分:3)

我发现我的收件箱中有些项目不是导致脚本暂停的邮件。 如果找到类似会议邀请的内容,这个小小的更改允许脚本继续运行:

Sub getmail()

Dim olApp As Outlook.Application
Dim objNS As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder

'Dim msg As Outlook.MailItem
Dim InboxItem As Object

Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("temp")

For Each InboxItem In olFolder.Items
    Debug.Print InboxItem.Subject
    Debug.Print InboxItem.EntryID
Next

End Sub

感谢您的回答!帮助了我很多!

(我道歉 - 想发表评论,但没有足够的代表......)

答案 2 :(得分:0)

要进一步向下钻取,请继续添加Set olFolder行:

Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("temp")
Set olFolder = olFolder.Folders("temp2")
Set olFolder = olFolder.Folders("temp3")

获取\ Inbox \ temp \ temp2 \ temp3 \