How do I mark emails already received as read when I get a second, different email based on subject which is different but similar?
So lets say I get an email with the subject:
[zenoss] **
A minute later I get an email with the subject:
[zenoss] CLEAR: **
Can someone help me figure out how fix my code below? Or fix my operator error... I've never made outlook macros so I might be failing at something obvious:
Const PST1_NAME = "test"
Const PST2_NAME = "test2"
Const FOLDER1_NAME = "Alerts - Zenoss"
Const FOLDER2_NAME = "Alerts - Zenoss - CLEAR"
Const CATEGORY_SEPERATOR = ","
' sample with hardcoded psts & folders
Private Sub markDuplicateEmails()
markDuplicates PST1_NAME & SEPERATOR & FOLDER1_NAME, PST2_NAME & SEPERATOR & FOLDER2_NAME, DEFAULT_CATEGORY
End Sub
' actual method which takes dynamic pst\folder source and destination
Public Sub markDuplicates(source, destination, category)
Dim myOlApp, myNameSpace
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Dim tmpArray, pst1Name, pst2Name, folder1Name, folder2Name
tmpArray = Split(source, SEPERATOR)
pst1Name = tmpArray(0)
folder1Name = tmpArray(1)
tmpArray = Split(destination, SEPERATOR)
pst2Name = tmpArray(0)
folder2Name = tmpArray(1)
Dim folder1Size, folder2Size
folder1Size = myNameSpace.Folders(pst1Name).Folders(folder1Name).Items.Count
folder2Size = myNameSpace.Folders(pst2Name).Folders(folder2Name).Items.Count
Dim array1(), array2()
ReDim array1(folder1Size)
ReDim array2(folder2Size)
Dim outlookItem1, outlookItem2, i, j
Dim theCstmData
'populate array1
i = -1
For Each outlookItem1 In myNameSpace.Folders(pst1Name).Folders(folder1Name).Items
i = i + 1
Set theCstmData.item = outlookItem1
theCstmData.Subject = outlookItem1.Subject(8, Subject.Length - 30)
array1(i) = theCstmData
DoEvents
Next outlookItem1
' populate array2
i = -1
For Each outlookItem2 In myNameSpace.Folders(pst2Name).Folders(folder2Name).Items
i = i + 1
Set theCstmData.item = outlookItem2
theCstmData.Subject = outlookItem2.Subject(15, Subject.Length - 30)
array2(i) = theCstmData
DoEvents
Next outlookItem2
'loop through each item in array1
For i = 0 To folder1Size - 1
'loop through each item in array 2 comparing each array2Item with current array1item
For j = 0 To folder2Size - 1
' if it is a match mark the item in array2 as duplicate
If array2(i).Subject = array1(i).Subject Then
If array1(i).item.UnRead = True Then
array1(i).item.UnRead = False
Else
array1(i).item.UnRead = array2(j).item.UnRead & CATEGORY_SEPERATOR
End If
array1(i).item.Save
If array2(j).item.UnRead = True Then
array2(j).item.UnRead = False
Else
array2(j).item.UnRead = array2(j).item.UnRead & CATEGORY_SEPERATOR
End If
array2(j).item.UnRead = False
array2(j).item.Save
End If
DoEvents
Next j
Next i
End Sub
So lets say I get an email with the subject:
[zenoss] **
A minute later I get an email with the subject:
[zenoss] CLEAR: **
Can someone help me figure out how fix my code below? Or fix my operator error... I've never made outlook macros so I might be failing at something obvious:
Const PST1_NAME = "test"
Const PST2_NAME = "test2"
Const FOLDER1_NAME = "Alerts - Zenoss"
Const FOLDER2_NAME = "Alerts - Zenoss - CLEAR"
Const CATEGORY_SEPERATOR = ","
' sample with hardcoded psts & folders
Private Sub markDuplicateEmails()
markDuplicates PST1_NAME & SEPERATOR & FOLDER1_NAME, PST2_NAME & SEPERATOR & FOLDER2_NAME, DEFAULT_CATEGORY
End Sub
' actual method which takes dynamic pst\folder source and destination
Public Sub markDuplicates(source, destination, category)
Dim myOlApp, myNameSpace
Set myOlApp = CreateObject("Outlook.Application")
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Dim tmpArray, pst1Name, pst2Name, folder1Name, folder2Name
tmpArray = Split(source, SEPERATOR)
pst1Name = tmpArray(0)
folder1Name = tmpArray(1)
tmpArray = Split(destination, SEPERATOR)
pst2Name = tmpArray(0)
folder2Name = tmpArray(1)
Dim folder1Size, folder2Size
folder1Size = myNameSpace.Folders(pst1Name).Folders(folder1Name).Items.Count
folder2Size = myNameSpace.Folders(pst2Name).Folders(folder2Name).Items.Count
Dim array1(), array2()
ReDim array1(folder1Size)
ReDim array2(folder2Size)
Dim outlookItem1, outlookItem2, i, j
Dim theCstmData
'populate array1
i = -1
For Each outlookItem1 In myNameSpace.Folders(pst1Name).Folders(folder1Name).Items
i = i + 1
Set theCstmData.item = outlookItem1
theCstmData.Subject = outlookItem1.Subject(8, Subject.Length - 30)
array1(i) = theCstmData
DoEvents
Next outlookItem1
' populate array2
i = -1
For Each outlookItem2 In myNameSpace.Folders(pst2Name).Folders(folder2Name).Items
i = i + 1
Set theCstmData.item = outlookItem2
theCstmData.Subject = outlookItem2.Subject(15, Subject.Length - 30)
array2(i) = theCstmData
DoEvents
Next outlookItem2
'loop through each item in array1
For i = 0 To folder1Size - 1
'loop through each item in array 2 comparing each array2Item with current array1item
For j = 0 To folder2Size - 1
' if it is a match mark the item in array2 as duplicate
If array2(i).Subject = array1(i).Subject Then
If array1(i).item.UnRead = True Then
array1(i).item.UnRead = False
Else
array1(i).item.UnRead = array2(j).item.UnRead & CATEGORY_SEPERATOR
End If
array1(i).item.Save
If array2(j).item.UnRead = True Then
array2(j).item.UnRead = False
Else
array2(j).item.UnRead = array2(j).item.UnRead & CATEGORY_SEPERATOR
End If
array2(j).item.UnRead = False
array2(j).item.Save
End If
DoEvents
Next j
Next i
End Sub