仅考虑第一个实例的Outlook vba循环

时间:2019-08-23 06:17:21

标签: vba outlook-vba

我有100封电子邮件,需要检查一个字符串,如果该字符串出现在电子邮件中,我们可以将其分类为“票证”,否则为“ NOTTICKETED”。为此,我有下面的VBA代码。该代码仅适用于第一封电子邮件,不适用于其余电子邮件。我无法弄清楚。为什么它不适用于所有电子邮件。 谁能帮忙。

Sub Find_String()
Dim olApp As Outlook.Application
Dim olExp As Outlook.Explorer
Dim olFolder As Outlook.MAPIFolder
Dim obj As Object
Dim i As Long
Dim x As Long
Dim count As Long
Dim regEx As Object
Set regEx = CreateObject("VBScript.RegExp")

Set olApp = Outlook.Application
Set olExp = olApp.ActiveExplorer
Dim olMatches As Object
Set olFolder = olExp.CurrentFolder

'Set count of email objects
count = olFolder.Items.count

'counter for emails
x = 1


regEx.Pattern = "(Consumer Number:.*\d{5})"
regEx.IgnoreCase = True
regEx.MultiLine = True
regEx.Global = True
Set olMatches = regEx.Execute(stremBody)

For Each obj In olFolder.Items

If regEx.test(obj.Body) Then
obj.Categories = "TICKETED"
    Else
obj.Categories = "NO TICKET"
End If
x = x + 1

Next obj

MsgBox ("All Emails checked")
ExitProc:
Set emItm = Nothing
Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing
End Sub

1 个答案:

答案 0 :(得分:1)

您需要在设置类别后保存消息:

If regEx.test(obj.Body) Then
    obj.Categories = "TICKETED"
Else
    obj.Categories = "NO TICKET"
End If
obj.Save