PDA

View Full Version : Move read items from Exchange Mailbox



rco3lho
11-27-2009, 12:21 PM
Hello,

I've searched this forum but no answer is complete for my problem (or at least I didn't find it).

I would like to automate a bit my Outlook. I have an Exchange account but my emails are delivered to my pst to control the size of the mailbox. The problem is that I use ActiveSync to check emails on my Sony Xperia phone when I'm away from my desk but because they're moved to the pst from time to time, I don't see them if Outlook is running.

I would like to learn a VBA code to pick the read messages from my Exchange Inbox and move them to my pst Inbox folder. If possible I would like this move to happen when closing Outlook (the same way that the deleted items are cleaned when closing Outlook). This way all the unread messages stay in my Exchange mailbox, I can set the delivery of new mail to happen to that mailbox and have it organized by the end of the day. If possible also to move the Sent Items from the Exchange to Sent Items in pst that would be great.

Thanks all for reading!
Greetings from Portugal

JP2112
11-27-2009, 05:35 PM
How can emails be moved from your Exchange Inbox to your local PST, if they are delivered directly to the PST?

rco3lho
11-28-2009, 04:40 AM
The idea is to return to the old situation of delivering the emails to the Exchange mailbox. I can control where do I want the emails to be delivered. Thanks

JP2112
11-28-2009, 04:20 PM
The problem is that by the time the Application_Quit event fires, all the GUIs have been closed and you will not have programmatic access to any existing folder or item. You would have to use a button (let's call it the "Quit" button) and develop a custom routine which would move the read emails into your local PST. You would also need to decide if you want to move read emails in subfolders to the local PST, and if so, where. For example, they could be moved to corresponding folders in the PST.

Overall it might be easier to move the messages after they are read, using the MailItem_Close event. See http://msdn.microsoft.com/en-us/library/aa171213%28office.11%29.aspx for sample code.

Note that neither of these approaches will work when you're reading emails on your phone.

rco3lho
11-30-2009, 01:49 AM
Ok. And if we forget about the "automatic close" since this can be done by a button before the actual close what would the approach be in terms of code? I also don't care for now about subfolders, it's enough for the mails to go from the Inbox in the Exchange side to the Inbox in my pst.

JP2112
11-30-2009, 02:39 PM
First you need programmatic access to your default Inbox, and then to the folder you want to move the unread items to. We loop through the default Inbox and move the read items (MailItem.UnRead property set to False).

i.e.

Dim Msg As Outlook.MailItem
Dim Itms As Outlook.Items
Dim i As Long
Dim MyPSTInbox As Outlook.MAPIFolder

Set Itms = GetItems(GetNS(GetOutlookApp), olFolderInbox)
Set MyPSTInbox = GetNS.Folders("Mailbox - Your Name").Folders("Inbox")

' need to step backwards in case we do need to move a msg
For i = Itms.Count To 1 Step -1
If IsMail(Itms.Item(i)) Then
' check unread property and move to other folder
If Itms.Item(i).UnRead = False Then
Itms.Item(i).Move MyPSTInbox
End If
End If
Next i

Function IsMail(itm As Object) As Boolean
IsMail = (TypeName(itm) = "MailItem")
End Function

Function GetOutlookApp() As Outlook.Application
' returns reference to native Application object
Set GetOutlookApp = Outlook.Application
End Function

Function GetNS(ByRef app As Outlook.Application) _
As Outlook.NameSpace
Set GetNS = app.GetNamespace("MAPI")
End Function

Function GetItems(olNS As Outlook.NameSpace, _
folder As OlDefaultFolders) As Outlook.Items
Set GetItems = olNS.GetDefaultFolder(folder).Items
End Function

You'll need to edit the reference to MyPSTInbox to make sure it points to the correct location.

rco3lho
12-03-2009, 02:53 AM
Hi JP!

First of all many thanks for helping me out.
I bumped into a small problem when trying to run the code:

Because I can't yet post links the picture is at:
rco3lho(dot)700megs(dot)com/Misc/Clipboard01.jpg

You can see the structure of my Outlook. The idea is to deliver the new email to Mailbox - Coelho, Rui Pedro (GE Indust, ConsInd) and then move it by code to Personal Folders Inbox (D:\Mail\Personal Folders.pst)

Thanks again!

JP2112
12-03-2009, 05:07 AM
My bad, GetNS takes an argument. That line should be

Set MyPSTInbox = GetNS(GetOutlookApp).Folders ... etc ...

rco3lho
12-03-2009, 05:43 AM
This is done by creating a new Macro in Tools/Macro/Macros correct?

rco3lho
12-03-2009, 05:52 AM
If I insert the line you sent me and modify the code to be like this:

Sub Move()
Dim Msg As Outlook.MailItem
Dim Itms As Outlook.Items
Dim i As Long
Dim MyPSTInbox As Outlook.MAPIFolder

Set Itms = GetItems(GetNS(GetOutlookApp), olFolderInbox)
Set MyPSTInbox = GetNS(GetOutlookApp).Folders("Inbox")

' need to step backwards in case we do need to move a msg
For i = Itms.Count To 1 Step -1
If IsMail(Itms.Item(i)) Then
' check unread property and move to other folder
If Itms.Item(i).UnRead = False Then
Itms.Item(i).Move MyPSTInbox
End If
End If
Next i
End Sub
Function IsMail(itm As Object) As Boolean
IsMail = (TypeName(itm) = "MailItem")
End Function

Function GetOutlookApp() As Outlook.Application
' returns reference to native Application object
Set GetOutlookApp = Outlook.Application
End Function

Function GetNS(ByRef app As Outlook.Application) _
As Outlook.NameSpace
Set GetNS = app.GetNamespace("MAPI")
End Function

Function GetItems(olNS As Outlook.NameSpace, _
folder As OlDefaultFolders) As Outlook.Items
Set GetItems = olNS.GetDefaultFolder(folder).Items
End Function

I get this error:

Run-time error '-2147221233 (8001010f)':

The operation failed. An object could not be found

Am I doing something wrong?

JP2112
12-03-2009, 04:43 PM
MyPSTInbox has point to the Inbox in your local PST, not your default Inbox (the one on the Exchange server).

Set MyPSTInbox = GetNS.Folders("Mailbox - Your Name").Folders("Inbox")

You have to physically look at your mailbox name in the folder tree ("Mailbox - Your Name") and see what it says, and edit the above line accordingly. For example mine says "Mailbox - Jimmy Pena" so that line (for me) would be

Set MyPSTInbox = GetNS.Folders("Mailbox - Jimmy Pena").Folders("Inbox")

rco3lho
12-06-2009, 05:56 AM
Hi JP,

I managed to get somewhere based on you code but the code bellow it's doing the opposite of what I need since it's moving the items from my PST to my Exchange Inbox. What I need is to move from the Exchange to my pst. Can you identify what's wrong?
Thanks

Sub Move()
Dim Msg As Outlook.MailItem
Dim Itms As Outlook.Items
Dim i As Long
Dim MyPSTInbox As Outlook.MAPIFolder

Dim objNS As Outlook.NameSpace
Set objNS = GetNS(Me.Application)
Set Itms = GetItems(GetNS(GetOutlookApp), olFolderInbox)
Set MyPSTInbox = objNS.Folders("Mailbox - Coelho, Rui Pedro (GE Indust, ConsInd)").Folders("Inbox")

' need to step backwards in case we do need to move a msg
For i = Itms.Count To 1 Step -1
If IsMail(Itms.Item(i)) Then
' check unread property and move to other folder
If Itms.Item(i).UnRead = False Then
Itms.Item(i).Move MyPSTInbox
End If
End If
Next i
End Sub
Function IsMail(itm As Object) As Boolean
IsMail = (TypeName(itm) = "MailItem")
End Function

Function GetOutlookApp() As Outlook.Application
' returns reference to native Application object
Set GetOutlookApp = Outlook.Application
End Function

Function GetNS(ByRef app As Outlook.Application) _
As Outlook.NameSpace
Set GetNS = app.GetNamespace("MAPI")
End Function

Function GetItems(olNS As Outlook.NameSpace, _
folder As OlDefaultFolders) As Outlook.Items
Set GetItems = olNS.GetDefaultFolder(folder).Items
End Function

JP2112
12-07-2009, 04:40 PM
Did you redirect your incoming emails to the Exchange account, instead of the local PST?

rco3lho
12-07-2009, 05:23 PM
No, not yet. The delivery of new mail is still in the personal folders inbox.

To be fair it goes first to the Exchange inbox but then it's automaticaly downloaded to Personal Folders.

Someone in other forum told me:
"Have you tried changing your first loop to use the items in MyPSTInbox, instead of your Exchange Inbox items?
Your current loop goes through the items in your exchange inbox.

Since I use Outlook 2007, I can't test out any code, but there's probably an Items property in MyPSTInbox and if so, definitely a Count property as well. Use that Count to loop through instead of Itms.Count, and check the Item in MyPSTInbox, instead of the item in Itms."

But I have no idea what that means :bug:

JP2112
12-09-2009, 05:56 PM
I'm lost.

How can you use code that forwards emails from your Exchange Inbox to your local PST, if the mail is being delivered directly to your local PST? Or is the mail being delivered to the Exchange Inbox and then being forwarded to your local PST?

rco3lho
12-10-2009, 02:25 AM
Hello,

I'm sorry if at any moment I wasn't clear.

As I told before I'm in control over where do I want the mail to be delivered. At the moment is directed to my PST but I can revert to be delivered and stored in Exchange Mailbox until I decide to move it. If I choose for the new mail to be delivered in my PST what happens is that email arrives at my Exchange mailbox and whenever Outlook is open it gets forwarded to my PST. Explanation: "If you have an Exchange account, your mailbox stores your email directly on the server so you can get to your stored email messages from any computer. However, when you connect to the Exchange server using Outlook with mail delivery directed to your personal folders, unread mail will be moved into the Inbox of your personal folders instead of being stored on the Exchange server."

What I'm after is for the mail to stay in Exchange until I run the code that will move READ mail to my PST. With the last code I shown is doing the opposite, it takes READ mail from my PST Inbox and places it in my Exchange Inbox.

Hope I was clear enough
Thanks!

rco3lho
12-17-2009, 02:06 AM
Just to keep it alive :)