How do I mark emails already received as read when I get a second, different email based on subject?

Status
Not open for further replies.

Mari

New Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
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
 
I can't say whether or not the logic works. However, if it is expected to run as VBA, then the syntax as far as we can see it isn't ok. This is hard to say as all the variables are declared as Variants instead of the really used types.

Dim theCstmData
The above dims a variable as Variant, which has no properties itself. As the variable isn't set to an object, this must fail because the item property doesn't exist:
Set theCstmData.item = outlookItem1
You'd need to add a class module to the VBA project, and add all the properties to it that are used in the code.

Also, the Outlook item, which is referenced in the for each loops, has a Subject property, which is a string that doesn't take any paramters. So
outlookItem1.Subject(8, Subject.Length - 30)
must fail.

No matter which programing language is used, this one certainly makes no sense at all:
array1(i).item.UnRead = array2(j).item.UnRead & CATEGORY_SEPERATOR
Joining True/False, or -1/0 respectively, with a string results in a string, and writing anything but "0" to a boolsche variable (Unread) will always result in True.

I'd say, contact your operator and have him fix the code, or get your money back.
 
heh there was no money. I tried to break someones free code down from online and format it to what I need. But I'm as you can see, not a programmer, so I tried to convert data that is a string and I apparently deleted references accidentally as well, I'll look into those to see what I deleted and see if I can patch it together
 
I use this - http://www.slipstick.com/developer/delete-older-messages-new-message-arrives/ - it's used in a run a script rule.

Do you need to watch a lot of folders? Looping through all folders looking for a match can be time consuming, especially if you have a lot of folders. If you're doing this with each new message, it's likely some will be skipped.

As long as all older messages with [zenoss] in the subject are to be marked read, you can use either instr or left to lool for a match.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
K mark emails with colour manually (like in thunderbird) Using Outlook 1
DoctorJellybean Outlook 365 doesn't always mark emails as read Using Outlook 3
C Mark all incoming emails as read Outlook VBA and Custom Forms 3
V Mark BCC when emails is sent from specific account Outlook VBA and Custom Forms 2
N Mark or follow up emails in shared mailbox Using Outlook 2
I Pound Symbol in emails show as � (black diamond with white question mark inside) - Office 2010. Using Outlook 11
Kika Melo How to mark as Junk any message not from Contacts (in Outlook.com) Using Outlook 3
V Macro to mark email with a Category Outlook VBA and Custom Forms 4
O Office 365 Outlook - mark to download Using Outlook 0
S Codes for "Mark Complete" the task Outlook VBA and Custom Forms 2
S Mark as Unread unless Categorized by Color Using Outlook 0
Diane Poremsky Mark Sent Items as Read After Copying with a Rule Using Outlook 0
M Mark Complete keyboard shortcut... on a mac running Windows Bootcamp Using Outlook 0
T Adding "Mark As Complete" btton to Task Remindet Pop-Up Using Outlook 3
F Automatically close email after selecting mark unread Using Outlook 1
T Outlook 2013 either fails to mark messages as read or marks them as read, then un-marks them. Using Outlook 1
Mary B VBscript: Need to copy every email to a folder & mark that copy as read Outlook VBA and Custom Forms 5
T outlook 2013 does not mark multiple forward messages as forward Using Outlook 2
7 Macro to mark message as junk and delete Outlook VBA and Custom Forms 3
B Outlook 2010: how to mark messages without opening? Eg for deletion Using Outlook 2
Z Mark moved copies as "Read" Using Outlook 19
Z "Mark All as Read" in Outlook 2007 extremely slow Using Outlook 1
H mark as read per folder options Using Outlook 1
C Outlook 2010 search for question mark in calendar Using Outlook 7
M Can't mark appointments as private for other user Using Outlook 1
A Mark all as read for public folders Using Outlook 14
N Changing the 'Mark item as read when selection changes' at runtime Outlook VBA and Custom Forms 1
B Move Sent Email to archival pst folder and mark as read - HOW TO Outlook VBA and Custom Forms 2
B Mark copy of sent email as read - VBA? Outlook VBA and Custom Forms 3
J Pasting into Outlook 2003 produces blue question mark Using Outlook 2
J Mark as read Outlook VBA and Custom Forms 2
M Mark as read Outlook VBA and Custom Forms 5
R Macro to mark item unread Outlook VBA and Custom Forms 2
T Setting a delegate to not mark items as read Using Outlook 9
M Outlook 365 VBA Auto-Forward Only the first of Duplicate Emails Outlook VBA and Custom Forms 2
J Action based on number of emails after sort by from Using Outlook 5
D Can't read some emails - text size too small. Using Outlook 2
R How to use VB .Net to send emails? Outlook VBA and Custom Forms 0
T Outlook is categorizing emails incorrectly Using Outlook 1
H Move Selected emails to Local Drive Outlook VBA and Custom Forms 0
D Delete Outlook emails from MS server Using Outlook 17
G Creating Macro to scrape emails from calendar invite body Outlook VBA and Custom Forms 6
A Flagged Emails highlighted in yellow Using Outlook 2
P Search folder: all emails sent to or from a domain Using Outlook 1
J Macro to Reply to Emails w/ Template Outlook VBA and Custom Forms 3
G Save emails as msg file from Outlook Web AddIn (Office JS) Outlook VBA and Custom Forms 0
T Outlook 2010 Sub accounts not showing new emails in Inbox Using Outlook 4
Nufc1980 Outlook "Please treat this as private label" auto added to some emails - Help. Using Outlook 4
humility36 Cannot move emails to archive - 440 error Outlook VBA and Custom Forms 1
E Edit incoming emails to remove a certain sentence added by the "system" Using Outlook 1

Similar threads

Back
Top