VBA to select mailbox if an account has multiple mailbox's

后端 未结 2 1653
孤城傲影
孤城傲影 2021-01-19 05:40

Here is my requirement.

I have multiple accounts in my OUTLOOK configured. 1) 1@email.com (only one mailbox) 2) 2@email.com (Multiple mailbox\'s are there. ex: Unix

相关标签:
2条回答
  • 2021-01-19 06:18

    Expanding on DanL's suggestion to loop through ns.Folders as I cannot tell whether you understood it.

    Option Explicit
    
    Sub accTopFolder()
    
    Dim oAccount As Account
    Dim ns As Namespace
    Dim fldr As folder
    Dim item As Object
    Dim inbx As folder
    
    Set ns = GetNamespace("MAPI")
    
    For Each oAccount In Session.Accounts
    
        Debug.Print vbCr & "oAccount: " & oAccount
        '
        For Each fldr In ns.Folders
        ' Shows all the names so you can replace "test"
            Debug.Print " top folder: " & fldr.name
            If fldr = "test" Then
                Set inbx = fldr.Folders("Inbox")
                'inbx.Display
                For Each item In inbx.Items
                    Debug.Print "  item .Subject: " & item.subject
                Next
                Exit For
            End If
        Next
    Next
    
    Set inbx = Nothing
    Set ns = Nothing
    
    End Sub
    
    0 讨论(0)
  • 2021-01-19 06:26

    You can use the DeliveryStore property of the Account to get its inbox. For example:

    Dim ns As NameSpace
    Set ns = Application.Session
    
    Dim acc As Account
    Dim f As Folder
    
    For Each acc In ns.Accounts
        ... Preconditions here ...
        Set f = acc.DeliveryStore.GetDefaultFolder(olFolderInbox)
        ... Now, do some looping ...
    Next
    
    0 讨论(0)
提交回复
热议问题