我正在尝试将NON-Default outlook帐户的垃圾邮件文件夹中的所有电子邮件转储到收件箱中,以便我可以在电子邮件上执行其他逻辑。 但是,我无法弄清楚如何引用垃圾邮件箱甚至是非默认帐户的收件箱,即使帐户已经检查,我的代码仍会通过我的默认帐户。
Public Sub New_Mail()
Dim oAccount As Outlook.Account
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
dim lngCount as long
lngcount = 0
For Each oAccount In Application.Session.Accounts ' cycle through accounts till we find the one we want
If oAccount = "desired.account@domain.ca" Then
Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderJunk) ' select junk folder of the account
Set objDestFolder = objNamespace.GetDefaultFolder(olFolderInbox) ' select inbox of the account
For lngCount = objSourceFolder.Items.Count To 1 Step -1 ' Go through all items in inbox, if a mail object, move into inbox
Set objVariant = objSourceFolder.Items.Item(dblCount)
DoEvents
If objVariant.Class = olMail Then
Set objCurrentEmail = objVariant ' the inbox item is an email, so change object type to olMail (email object)
objCurrentEmail.Categories = "red category"
objCurrentEmail.Move objDestFolder ' Move the email to the required folder
End If
Next
End If
Next
End Sub
编辑: 在Eric的回答之后,我想分享我现在正在使用的代码。
Private Sub clearJunk()
Dim objVariant As Variant ' Variant object to handle and inbox item
Dim objCurrentEmail As Outlook.MailItem ' Temporary email object for logic
Dim dblCount As Double ' Double used to count email items in the inbox
Dim objStore As Outlook.Store ' Store Object to cycle through email accounts
Dim objRoot As Outlook.Folder ' Folder object to define Inbox of desired account
Dim folders As Outlook.folders ' FolderS object to holder folders...lol
Dim Folder As Outlook.Folder ' Temporary Folder object
Dim foldercount As Integer ' integer to count folders in account
Dim objInboxFolder As Outlook.MAPIFolder ' MAPI folder object to move emails to or from
Dim objJunkFolder As Outlook.MAPIFolder ' MAPI folder object to move emails to or from
Dim objRandomFolder As Outlook.MAPIFolder ' MAPI folder object to move emails to or from
'--------------------------------------------------------------------
' Cycle through each account in outlook client and find desired account
For Each objStore In Application.Session.Stores
If objStore = "desired.account@domain.ca" Then ' If we find the account
Set objRoot = objStore.GetRootFolder ' Store int objRoot Object
On Error Resume Next
Set folders = objRoot.folders ' Check if it has folders
foldercount = folders.Count
If foldercount Then ' if folders exist
For Each Folder In folders ' Go through each folder AND ....
' Look for Junk Email folder, Inbox Folder, and some random customer folder.
' Store in individual objects for future referencing
If Folder.FolderPath = "\\desired.account@domain.ca\Junk Email" Then
Set objJunkFolder = Folder
End If
If Folder.FolderPath = "\\desired.account@domain.ca\Inbox" Then
Set objInboxFolder = Folder
End If
If Folder.FolderPath = "\\desired.account@domain.ca\Random Custom Folder" Then
Set objRandomFolder = Folder
End If
Next
End If
' Now we have everything identified lets move emails!
For dblCount = objJunkFolder.Items.Count To 1 Step -1
Set objVariant = objJunkFolder.Items.Item(dblCount)
DoEvents
If objVariant.Class = olMail Then
Set objCurrentEmail = objVariant
objCurrentEmail.Categories = "Red Category"
objCurrentEmail.Move objInboxFolder
End If
Next
End If
Next
End Sub
答案 0 :(得分:0)
您需要为非默认帐户调用Store.GetDefaultFolder(olFolderInbox)。从Account.DeliveryStore属性中获取Store对象 - 在大多数情况下,这将是正确的商店,除非它是一个PST帐户,将邮件传递到另一个帐户的商店(甚至可能是默认帐户' s)存储)。