Outlook Banter

Outlook Banter (http://www.outlookbanter.com/)
-   Outlook and VBA (http://www.outlookbanter.com/outlook-vba/)
-   -   macro that checks if email is duplicate (http://www.outlookbanter.com/outlook-vba/33409-macro-checks-if-email-duplicate.html)

Rayo K November 20th 06 10:53 PM

macro that checks if email is duplicate
 
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

Michael Bauer [MVP - Outlook] November 21st 06 06:00 AM

macro that checks if email is duplicate
 


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


Rayo K November 21st 06 04:19 PM

macro that checks if email is duplicate
 
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 [MVP - Outlook]" wrote:



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



Michael Bauer [MVP - Outlook] November 22nd 06 05:51 AM

macro that checks if email is duplicate
 

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 [MVP - Outlook]" wrote:



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



Rayo K November 22nd 06 01:34 PM

macro that checks if email is duplicate
 
Yes, you are right. I was thinking about VB.NET. Thank you, it is now fixed.

"Michael Bauer [MVP - Outlook]" wrote:


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 [MVP - Outlook]" wrote:



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




All times are GMT +1. The time now is 11:21 AM.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2006 OutlookBanter.com