Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
988 views
in Technique[技术] by (71.8m points)

vba - Access Outlook default folder

I working on an Outlook VBA application and I need to access my inbox but I seem to be having some trouble. I am using the GetDefaultFoldder(olFolderInbox) method, however, I have several email addresses set up and none of them show up in my personal folder's inbox.

So my question is, where is this default folder defined? How do I know which inbox is the default one? I know there is also the GetFolderFromID method, if I were to use this,

how can I find a folders ID in order to point to it?

Here is the code I am using. This is from a tutorial on Timothy Chen Allen's blog as seen here Timothy's Blog. The code:

Sub find_unread()
    On Error GoTo eh:
    Dim ns As Outlook.NameSpace
    Dim folder As MAPIFolder
    Dim item As Object
    Dim msg As MailItem

    Set ns = Session.Application.GetNamespace("MAPI")
    Set folder = ns.GetDefaultFolder(olFolderInbox)

    For Each item In folder.Items
        DoEvents
        If (item.Class = olMail) And (item.UnRead) Then
            Set msg = item
            Debug.Print msg.SenderEmailAddress
            msg.Display True
        End If
    Next

    MsgBox "All messages in Inbox are read", vbInformation, "All Read"
    Exit Sub
eh:
    MsgBox Err.Description, vbCritical, Err.Number
End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the Folders property, and string multiple Folders properties together, to get at any folder in the namespace. Some examples

The Inbox (same as GetDefaultFolder(olInbox))

ns.Folders("Personal Folders").Folders("Inbox")

A subfolder of Inbox named Backup

ns.Folders("Personal Folders").Folders("Inbox").Folders("Backup")

The OtherInbox at the same level as Personal Folders

ns.Folders("OtherInbox")

The GetDefaultFolder is good for quickly getting to a default folder, but if you need something other than the default, just navigate down the tree with the Folders property of the NameSpace object.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...