我正在构建一个功能,用于对每天在共享电子邮件收件箱中收到的大量自动邮件进行排序。我使用循环遍历收件箱中的每封邮件和IF语句来过滤/排序邮件,然后运行此功能:
Function MoveSort(olDestination As Outlook.Folder)
Dim StCategory
If oItems.Item(i).Categories = "" Then
oItems.Item(i).Categories = "Category"
End If
oItems.Item(i).UnRead = False
oItems.Item(i).Save
oItems.Item(i).Move olDestination
End Function
一切正常,但通常不会分配类别。 (有时它似乎会随机将它分配给第一条或最后一条消息。)
这是主要程序:
Private i As Integer
Private oItems As Outlook.Items
Sub OrganizeIt()
Dim oNS As Outlook.NameSpace
Dim oInbox As Outlook.Folder
Dim oBStock As Outlook.Folder
Dim oCStock As Outlook.Folder
Dim oStock As Outlook.Folder
Dim SEmail As String
Dim SSubject As String
Set oNS = Application.GetNamespace("MAPI")
Set oInbox = oNS.folders("HelpDeskEmail").folders("Inbox")
Set oItems = oInbox.Items
Set oCStock = oInbox.folders("Folder1")
Set oBStock = oInbox.folders("Folder2")
For i = oItems.Count To 1 Step -1
SEmail = oItems.Item(i).SenderEmailAddress
SSubject = oItems.Item(i).Subject
If SEmail = "Email1@email.com" Or SSubject = "Sample Subject 1" Or _
Left(SSubject, 16) = "Sample Subject 2" Then
MoveSort oStock
ElseIf SEmail = "Email2@email.com" Then
MoveSort oBStock
ElseIf SEmail = "Email3@email.com" Then
MoveSort oCStock
End If
Next i
End Sub
答案 0 :(得分:0)
这是多点点符号 - 下面的代码在一个对象上设置Unread属性,并在一个完全不同的对象上调用Save,该对象不知道你的代码以前做过的任何事情。
oItems.Item(i).UnRead = False
oItems.Item(i).Save
不要使用多点符号并且不要使i成为全局变量 - 将对象作为参数传递
Function MoveSort(olDestination As Outlook.Folder, Item as Object)
Dim StCategory
If Item.Categories = "" Then
Item.Categories = "Category"
End If
Item.UnRead = False
Item.Save
Item.Move olDestination
End Function
...
Dim Item as Object
For i = oItems.Count To 1 Step -1
set Item = oItems.Item(i)
SEmail = Item.SenderEmailAddress
SSubject = Item.Subject
If SEmail = "Email1@email..com" Or SSubject = "Sample Subject 1" Or _
Left(SSubject, 16) = "Sample Subject 2" Then
MoveSort oStock, Item
ElseIf SEmail = "Email2@email.com" Then
MoveSort oBStock, Item
ElseIf SEmail = "Email3@email.com" Then
MoveSort oCStock, Item
End If
Next i