macro that checks if email is duplicate

R

Rayo K

Hello,

I am trying to write a macro that will inspect a certain folder, and if it
contains an email that is not already in the "Saved" folder, copy that email.

Right now I am having trouble identifiying an email as a duplicate item. Is
there a routine taht will quickly identify two Mailitems as being the same?
Right now I am comparing receivedtime, subject, and sender properties one at
a time.

Thanks
 
M

Michael Bauer [MVP - Outlook]

Outlook doesn't provide you with such a routine, you have to write it
yourself.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
-- www.VBOffice.net --

Am Mon, 20 Nov 2006 14:53:02 -0800 schrieb Rayo K:
 
R

Rayo K

OK,

I changed my code so it uses a While statement, but I keep getting and error
saying I can't use Exit While or End While. Is "While" different in VBA than
VB?

Here is my code:

Sub main()

'handles for manipulated objects
Dim app As Outlook.Application
Dim space As NameSpace
Dim box1 As MAPIFolder
Dim box2 As MAPIFolder
Dim folder1 As MAPIFolder
Dim folder2 As MAPIFolder
Dim itemcoll1 As Items
Dim itemcoll2 As Items
Dim item1 As MailItem
Dim item2 As MailItem

Dim x As Integer, y As Integer
Dim numItems As Integer
Dim FCont As Boolean
Dim FSame As Boolean



'define the handles
Set app = CreateObject("Outlook.Application")
Set space = MyApp.GetNamespace("MAPI")
Set box1 = MyNameSpace.Folders("Test1")
Set folder1 = box1.Folders("Mail1")
Set box2 = MyNameSpace.Folders("Test2")
Set folder2 = box2.Folders("Mail2")
Set itemcoll1 = folder1.Items
Set itemcoll2 = folder2.Items

'sort items collections by received date
itemcoll1.Sort "[ReceivedTime]", True
itemcoll2.Sort "[ReceivedTime]", True


'step through all items in collection 1
numItems = itemcoll1.Count

For x = 1 To numItems
Set item1 = itemcoll1(x)
y = 1
FCont = True
'go through items in folder 2
While FCont
'If there is no item corresponding to y, then the email is not in the
folder.
If y > itmecoll2.Count Then
item1.Copy.Move folder2
exit while
End If

Set item2 = itemcoll2(y)
'If the received time is earlier than the item in folder 2, go to the
next one. _
Otherwise check if they are the same

If item1.ReceivedTime >= item2.ReceivedTime Then
FCont = False

'Check if emails are the same by received time, subject, and sender

FSame = True
If Not (item1.Subject = item2.Subject) Then FSame = False
End If
If Not (item1.ReceivedTime = item2.ReceivedTime) Then FSame = False
End If
If Not (item1.SenderEmailAddress = item2.SenderEmailAddress) Then
FSame = False
End If
'If any of these were not the same, the items are different
If FSame = False Then
item1.Copy.Move folder2
End If
End If
'if the item2 was earlier find one that is later.
y = y + 1
End While

Next x

End Sub
 
M

Michael Bauer [MVP - Outlook]

The loops are the same in VB and VBA, maybe you're talking about VB.Net?
That's not VB.

You can use:

While ...
...
Wend

If you need an Exit statement use:

Do While ...
...
If ... Then Exit Do
Loop

Or:

For i=1 To x
...
If ... Then Exit For
Next

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
-- www.VBOffice.net --


Am Tue, 21 Nov 2006 08:19:01 -0800 schrieb Rayo K:
OK,

I changed my code so it uses a While statement, but I keep getting and error
saying I can't use Exit While or End While. Is "While" different in VBA than
VB?

Here is my code:

Sub main()

'handles for manipulated objects
Dim app As Outlook.Application
Dim space As NameSpace
Dim box1 As MAPIFolder
Dim box2 As MAPIFolder
Dim folder1 As MAPIFolder
Dim folder2 As MAPIFolder
Dim itemcoll1 As Items
Dim itemcoll2 As Items
Dim item1 As MailItem
Dim item2 As MailItem

Dim x As Integer, y As Integer
Dim numItems As Integer
Dim FCont As Boolean
Dim FSame As Boolean



'define the handles
Set app = CreateObject("Outlook.Application")
Set space = MyApp.GetNamespace("MAPI")
Set box1 = MyNameSpace.Folders("Test1")
Set folder1 = box1.Folders("Mail1")
Set box2 = MyNameSpace.Folders("Test2")
Set folder2 = box2.Folders("Mail2")
Set itemcoll1 = folder1.Items
Set itemcoll2 = folder2.Items

'sort items collections by received date
itemcoll1.Sort "[ReceivedTime]", True
itemcoll2.Sort "[ReceivedTime]", True


'step through all items in collection 1
numItems = itemcoll1.Count

For x = 1 To numItems
Set item1 = itemcoll1(x)
y = 1
FCont = True
'go through items in folder 2
While FCont
'If there is no item corresponding to y, then the email is not in the
folder.
If y > itmecoll2.Count Then
item1.Copy.Move folder2
exit while
End If

Set item2 = itemcoll2(y)
'If the received time is earlier than the item in folder 2, go to the
next one. _
Otherwise check if they are the same

If item1.ReceivedTime >= item2.ReceivedTime Then
FCont = False

'Check if emails are the same by received time, subject, and sender

FSame = True
If Not (item1.Subject = item2.Subject) Then FSame = False
End If
If Not (item1.ReceivedTime = item2.ReceivedTime) Then FSame = False
End If
If Not (item1.SenderEmailAddress = item2.SenderEmailAddress) Then
FSame = False
End If
'If any of these were not the same, the items are different
If FSame = False Then
item1.Copy.Move folder2
End If
End If
'if the item2 was earlier find one that is later.
y = y + 1
End While

Next x

End Sub





Michael Bauer said:
Outlook doesn't provide you with such a routine, you have to write it
yourself.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
-- www.VBOffice.net --

Am Mon, 20 Nov 2006 14:53:02 -0800 schrieb Rayo K:
one
at
 
R

Rayo K

Yes, you are right. I was thinking about VB.NET. Thank you, it is now fixed.

Michael Bauer said:
The loops are the same in VB and VBA, maybe you're talking about VB.Net?
That's not VB.

You can use:

While ...
...
Wend

If you need an Exit statement use:

Do While ...
...
If ... Then Exit Do
Loop

Or:

For i=1 To x
...
If ... Then Exit For
Next

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
-- www.VBOffice.net --


Am Tue, 21 Nov 2006 08:19:01 -0800 schrieb Rayo K:
OK,

I changed my code so it uses a While statement, but I keep getting and error
saying I can't use Exit While or End While. Is "While" different in VBA than
VB?

Here is my code:

Sub main()

'handles for manipulated objects
Dim app As Outlook.Application
Dim space As NameSpace
Dim box1 As MAPIFolder
Dim box2 As MAPIFolder
Dim folder1 As MAPIFolder
Dim folder2 As MAPIFolder
Dim itemcoll1 As Items
Dim itemcoll2 As Items
Dim item1 As MailItem
Dim item2 As MailItem

Dim x As Integer, y As Integer
Dim numItems As Integer
Dim FCont As Boolean
Dim FSame As Boolean



'define the handles
Set app = CreateObject("Outlook.Application")
Set space = MyApp.GetNamespace("MAPI")
Set box1 = MyNameSpace.Folders("Test1")
Set folder1 = box1.Folders("Mail1")
Set box2 = MyNameSpace.Folders("Test2")
Set folder2 = box2.Folders("Mail2")
Set itemcoll1 = folder1.Items
Set itemcoll2 = folder2.Items

'sort items collections by received date
itemcoll1.Sort "[ReceivedTime]", True
itemcoll2.Sort "[ReceivedTime]", True


'step through all items in collection 1
numItems = itemcoll1.Count

For x = 1 To numItems
Set item1 = itemcoll1(x)
y = 1
FCont = True
'go through items in folder 2
While FCont
'If there is no item corresponding to y, then the email is not in the
folder.
If y > itmecoll2.Count Then
item1.Copy.Move folder2
exit while
End If

Set item2 = itemcoll2(y)
'If the received time is earlier than the item in folder 2, go to the
next one. _
Otherwise check if they are the same

If item1.ReceivedTime >= item2.ReceivedTime Then
FCont = False

'Check if emails are the same by received time, subject, and sender

FSame = True
If Not (item1.Subject = item2.Subject) Then FSame = False
End If
If Not (item1.ReceivedTime = item2.ReceivedTime) Then FSame = False
End If
If Not (item1.SenderEmailAddress = item2.SenderEmailAddress) Then
FSame = False
End If
'If any of these were not the same, the items are different
If FSame = False Then
item1.Copy.Move folder2
End If
End If
'if the item2 was earlier find one that is later.
y = y + 1
End While

Next x

End Sub





Michael Bauer said:
Outlook doesn't provide you with such a routine, you have to write it
yourself.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
-- www.VBOffice.net --

Am Mon, 20 Nov 2006 14:53:02 -0800 schrieb Rayo K:

Hello,

I am trying to write a macro that will inspect a certain folder, and if it
contains an email that is not already in the "Saved" folder, copy that
email.

Right now I am having trouble identifiying an email as a duplicate item.
Is
there a routine taht will quickly identify two Mailitems as being the
same?
Right now I am comparing receivedtime, subject, and sender properties one
at
a time.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top