Move applied to MailItem alters ReceivedTime

T

Thomas

Hy everybodey,

I wrote a pieve of code to move emails from one .pstn folder to anothor one.
Not difficult, but unfortunately the Date appears to be changed after the
move. It appears that after applying the move method the ReceivedTime
property shows the time of the move, rather than the time when the email was
received.

Simply overwriting the faulty time afterwards does not work either, because
ReceivedTime is write-protected for MailItem.

Does anybody know a workaround or are there some bugs in the piece of code I
attach?

Sub Move_to_old_pst()

Dim s As String 'For Debug message
Dim found As Boolean
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myNewFolder, myOldFolder, myNewSubFolder, myOldSubFolder As
Outlook.MAPIFolder
Dim myItem, myItem2, myItem3 As Object


Set myNameSpace = myOlApp.GetNamespace("MAPI")

' put in some existing opened .pst folders here, which are not harmed by
playing around
' Mailes are assumed to be in subfolders of the same name in both .pst folders
Set myNewFolder = myNameSpace.Folders("xx new")
Set myOldFolder = myNameSpace.Folders("xx")
GoSub MoveRoutine

Exit Sub


MoveRoutine:

For Each myNewSubFolder In myNewFolder.Folders

found = False
For Each myOldSubFolder In myOldFolder.Folders
If myOldSubFolder.Name = myNewSubFolder.Name Then
found = True
Exit For
End If
Next

If Not found Then
myOldFolder.Folders.Add (myNewSubFolder.Name)
Set myOldSubFolder = myOldFolder.Folders(myNewSubFolder.Name)
End If


' For debugging
s = myNewSubFolder.Name & vbCrLf & vbCrLf

For Each myItem In myNewSubFolder.Items

' For debugging: Original receive time
s = s & myItem.Subject & vbCrLf & myItem.ReceivedTime & vbCrLf

' >>> Here is the problematic move <<<<<
myItem.Move myOldSubFolder

' For debugging: Still Original receive time
s = s & myItem.ReceivedTime & vbCrLf

' For debugging only: Time of Move as receive time
Set myItem2 = myOldSubFolder.Items(myItem.Subject)
s = s & myItem2.ReceivedTime & vbCrLf
MsgBox s
Exit Sub

Next

Return

End Sub
 
E

Eric Legault [MVP - Outlook]

Are you sure it is not the LastModifiedTime property that's getting changed?
The Received Time is for sure always read-only except for mail servers.
 
D

Dmitry Streblechenko

Move is a function returning the new item, not a sub:
set myItem = myItem.Move(myOldSubFolder)

Are you moving messages between two folders from *different* PST stores?
Note that on the MAPI level, messages can be moved only within the same
store
(IMAPIFolder::CopyMessages(.., MESSAGE_MOVE)), in case of two different
stores, OOM will create a new item in the target folder, then copy all the
properties from the original message, then delete it.
In this case creation and last modification times will not be preserved
(which is expected), however the received time can be reset using MAPI/CDO
1.21/Redemption. Unless you are using a Public Folder, which does not allow
to modify the received and sender props unless you open the store using an
admin account.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
T

Thomas

Yes, I am concerned with the RecivedTime, because this one seems to be the
one displayed as "Date" in the Outllok folder. I checked that this thing is
beeing modified, see my code below.
I agree the LastModifiedTime should be changed.
 
T

Thomas

Thanks for your reply, see my answers and more questions below

Dmitry Streblechenko said:
Move is a function returning the new item, not a sub:
set myItem = myItem.Move(myOldSubFolder)

Seems to work both as sub or function, but the result (and my problem) is
not changed.
Are you moving messages between two folders from *different* PST stores?
Exactly

Note that on the MAPI level, messages can be moved only within the same
store

The Move command (function or subroutine) seems to work also for different
..pst folders, and it seems to behave exactly as you describe below, so you
are probably correct that it is implemented using IMAPIFolder::CopyMessages.
(IMAPIFolder::CopyMessages(.., MESSAGE_MOVE)), in case of two different
stores, OOM will create a new item in the target folder, then copy all the
properties from the original message, then delete it.

You are not recommending that I should use that function directly instead ov
Move? It seems it would not solve my problem.
In this case creation and last modification times will not be preserved
(which is expected), however the received time can be reset using MAPI/CDO
1.21/Redemption.

I assume you are referring to the redemption library described at your
webpage. However, I did not see how I could access the ReceivedTime for
MailItem with that.

Unless you are using a Public Folder, which does not allow
to modify the received and sender props unless you open the store using an
admin account.

No problem with that, it's private .pst folders in my case
 
D

Dmitry Streblechenko

I think you are missing my point
1. Of course it works in both cases. The Move() method does not care whether
you ignore the returned value or not. I was just pointing out that the new
item is returned by Move(), so you don't have to look for it in the target
folder. Your current code retrieves the new item using the value of the
Subject property, which is not very reliable to put it mildly.
2. I simply explained how Move is implemented by Outlook on the MAPI level
and why you are seeing this behavior. What I suggested is that you can still
use the Move method in OOM but reset the PR_CLIENT_SUBMIT_TIME and
PR_MESSAGE_DELIVERY_TIME properties on the target message using Extended
MAPI, CDO 1.21 or Redemption.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

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