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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8322432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 12:14:51  来源:igfitidea点击:

Using visual basic to access subfolder in Inbox?

vbaoutlookoutlook-vbasubdirectory

提问by kinkajou

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

I have used the code above to access the main outlook Inbox but how to access the folders in inbox and it's mail using vba!

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

回答by brettdj

Thats very close :)

那非常接近:)

To get all the mail items in a folder called "temp" under the Inbox try this

要获取收件箱下名为“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

回答by Casey Morter

I found that there were some items in my inbox that were not mail items causing the script to halt. This little change allowed the script to keep running if something like a meeting invite is found:

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

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

Thanks for your answer! helped me a lot!

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

(My apologies - wanted to comment, but don't have enough rep..)

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

回答by Greg Glynn

And to drill further down, keep adding Set olFolder lines:

为了进一步深入,继续添加 Set olFolder 行:

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

Gets you to \Inbox\temp\temp2\temp3\

让你到 \Inbox\temp\temp2\temp3\