Archive Mailitem

  • Thread starter Thread starter Hei
  • Start date Start date
H

Hei

hi,

i am doing a macro for archive mail from Exchange Server to personal (pst),
i have several problem at my program.

1. i would like to create PST file by macro ,and i want the folder name show
in outlook is same as file name, so i can easy management these Personal
Folder and file.now , the problem is after i create a personal folder (
myns.AddStore "c:\" &
format(date,'yyyymm') & ".pst")
i don't know how to rename the folder same as file name.

2. i use 'TypeOf myItem Is Outlook.MailItem' to determine items in inbox is
outlook.mailitem or not, it is always return 'false'. items in Inbox does
not mailitem??

Pls help.
Hei.
 
1) See http://www.outlookcode.com/codedetail.aspx?id=84. As the discussion
notes, you may need to remove the store after you create it and add it again
for the display name change to takes effect in the UI.

2) How are you instantiating myItem? Try checking the MailItem.Class
property instead of using TypeOf.
 
thanks for advise. in question 2 'myItem' is really not a outlook.mailitem
in declaration.
it is 'Object' type, but my code as follow

For Each myItem In mySourFolder.Items
If TypeOf myItem Is Outlook.MailItem = True Then
If myItem.ReceivedTime < ReceivedBefore Then _
myItem.Move myDestFolder
Else
MsgBox ("Item " & myItem.Subject & " not a mailitem")
Exit For
End If
Next

suppose myItem will to be a outlook.mailitem. it there some think wrong of
me?

and with above code if i remove the receivedtime condition, suppose will
move all items, but it just move a part of item without any error message.
when i run these code again, it move some of remain item... at last may be i
run this program for several time it will finish to move all items. ???

Hei.
 
Never move or delete items inside a For Each loop. Instead use a While ...
End While loop or a countdown loop:

myItems = mySourFolder.Items
intCount = myItems.Count
For i = intCount to 1 Step -1
myItem = myItems.Item(i)
If myItem.Class = Outlook.olObjectClass.olMail Then
If myItem.ReceivedTime < ReceivedBefore Then
myItem.Move myDestFolder
End If
Else
MsgBox ("Item " & myItem.Subject & " not a mailitem")
End If
Next

Note the use of the Class property to determine what type of item it is.
It's a bit more flexible than TypeOf, since it tells you what type of item
it is. For example, yoiu might want to simply delete any olReport items.

You might also want to use Items.Find or Items.Restrict to filter on
ReceivedTime instead of examining the date on every MailItem. See
http://www.outlookcode.com/d/finddate.htm
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
Back
Top