问题
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 box, Windows Box, Mac box)
Here my 2nd email account has its own mailbox and linked to multiple mailbox's like UNIX, Windows etc. Each Mailbox has its own inbox and sub folders.
Now i need to select a folder in Unix box (inbox) and run the code to do something in side the folder.
Here's what i have
For Each oAccount In Application.Session.Accounts
If oaccount ="1@email.com" then
Set folder = ns.GetDefaultFolder(olFolderInbox) ' here it selects the inbox folder of account.
For each item in folder.items
Code goes here
next
end if
next
This works fine for single mailbox account, but when i do this for multiple mailbox account , it doesn't work.
any help would be appreciated.
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/33953386/vba-to-select-mailbox-if-an-account-has-multiple-mailboxs