Using visual basic to access subfolder in Inbox?

左心房为你撑大大i 提交于 2019-11-26 14:33:57

问题


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!


回答1:


Thats very close :)

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

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



回答2:


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..)




回答3:


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

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\



来源:https://stackoverflow.com/questions/8322432/using-visual-basic-to-access-subfolder-in-inbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!