I'm incredibly frustrated with Outlook's utter inability to get one simple thing right.
I simply want to copy a mail item from Inbox to a special folder, without any duplicate. Easy as pie, no?
Except that by default, Outlook doesn't provide a unique key that can be used to identify whether one mailitem is same as another (without having to checking subject line, sender, and whatnots). I have chosen not to use Redemption or other third-party Add-Ins because that's just one more dependency for a simple functionality, so I'm trying to stick to what is provided by default as possible.
I was advised that I could accomplish this using UserProperties, which I can add my own property to. But I ran into a host of problems:
1) When I add a userproperty, I have to save the mail item, and if it was moved to another place prior to saving, I get an error saying that object has been deleted. (Why did it let me add userproperty without an error then?) I switched the order around so I added the property, then saved, then copy it to the folder. Works OK.
2) When I check for duplicates, this seems to be wonky. For example, implicitly referencing to the object doesn't work at all. If I reference the object directly, I can access the userproperty, but when I try to reference this implicitly (e.g. using the new item as a basis of comparsion), there is no userproperties to be found.
3) Using For Each...Next, which I was trying to avoid in first place seems to be utterly incapable of doing a comparison and thus pass off everything as duplicates even they aren't.
Here's the codes associated. Any insights will be so *MUCH* appreciated.
In Immediate windows:
Edit: I tried using For..Next with a Integer instead of For Each...Next and it works. But holy hell, this is fugly. I would really, really prefer to implicitly reference the object and go directly to that item without wasting cycles checking each mailitem....
I simply want to copy a mail item from Inbox to a special folder, without any duplicate. Easy as pie, no?
Except that by default, Outlook doesn't provide a unique key that can be used to identify whether one mailitem is same as another (without having to checking subject line, sender, and whatnots). I have chosen not to use Redemption or other third-party Add-Ins because that's just one more dependency for a simple functionality, so I'm trying to stick to what is provided by default as possible.
I was advised that I could accomplish this using UserProperties, which I can add my own property to. But I ran into a host of problems:
1) When I add a userproperty, I have to save the mail item, and if it was moved to another place prior to saving, I get an error saying that object has been deleted. (Why did it let me add userproperty without an error then?) I switched the order around so I added the property, then saved, then copy it to the folder. Works OK.
2) When I check for duplicates, this seems to be wonky. For example, implicitly referencing to the object doesn't work at all. If I reference the object directly, I can access the userproperty, but when I try to reference this implicitly (e.g. using the new item as a basis of comparsion), there is no userproperties to be found.
3) Using For Each...Next, which I was trying to avoid in first place seems to be utterly incapable of doing a comparison and thus pass off everything as duplicates even they aren't.
Here's the codes associated. Any insights will be so *MUCH* appreciated.
Code:
Private Function NoDuplicate(ByVal oFolder As Outlook.MAPIFolder, ByVal objitem As Outlook.MailItem) As Boolean
Dim oFind As Outlook.Items
Dim oItems As Outlook.Items
Dim oItem As Outlook.MailItem
Dim strSearch As String
Set oItems = oFolder.Items
If Not objitem.Parent = oFolder Then
On Error Resume Next
For Each oItem In oItems
'This comparison will not work even if "OriginalEntryID" is nothing while EntryID is something with value!
If oItem.UserProperties("OriginalEntryID") = objitem.EntryID Then
NoDuplicate = False
Exit Function
End If
Next oItem
GoTo CheckPassed
End If
Exit Function
CheckPassed:
NoDuplicate = True
End Function
Private Sub AddProperty(oItem As MailItem, oFolder As MAPIFolder, orEntryID As Variant)
With oItem.UserProperties
.Add "OriginalEntryID", olText
.Item("OriginalEntryID").Value = orEntryID
'oFolder.Items.Item(CStr(oItem)).Save <--Implicit reference will not work
End With
oItem.Save
End Sub
In Immediate windows:
Code:
?oitem.UserProperties.Count
0
?ofolder.Items.Count
3
?ofolder.Items(3).userproperties.count
1 <-- This is actually the same mailitem as oItem but oItem has no Userproperties?
Edit: I tried using For..Next with a Integer instead of For Each...Next and it works. But holy hell, this is fugly. I would really, really prefer to implicitly reference the object and go directly to that item without wasting cycles checking each mailitem....
Last edited: