如何将项目传递给函数以设置类别?
How to pass items to a function to set categories?
我正在构建一个功能,用于对共享电子邮件收件箱中每天收到的大量自动邮件进行排序。我使用一个循环遍历收件箱中的每封邮件消息和一个 IF 语句到 filter/sort 条消息,然后运行此函数:
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
这是一种极端的多点表示法 - 以下代码在一个对象上设置未读 属性 并在完全不同的对象上调用 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
我正在构建一个功能,用于对共享电子邮件收件箱中每天收到的大量自动邮件进行排序。我使用一个循环遍历收件箱中的每封邮件消息和一个 IF 语句到 filter/sort 条消息,然后运行此函数:
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
这是一种极端的多点表示法 - 以下代码在一个对象上设置未读 属性 并在完全不同的对象上调用 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