Consulting

Results 1 to 17 of 17

Thread: Move read items from Exchange Mailbox

  1. #1

    Move read items from Exchange Mailbox

    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

  2. #2
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    How can emails be moved from your Exchange Inbox to your local PST, if they are delivered directly to the PST?
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  3. #3
    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

  4. #4
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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/libr...ice.11%29.aspx for sample code.

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

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  5. #5
    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.

  6. #6
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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.

    [vba]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[/vba]

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

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  7. #7
    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!

  8. #8
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    My bad, GetNS takes an argument. That line should be

    Set MyPSTInbox = GetNS(GetOutlookApp).Folders ... etc ...
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  9. #9
    This is done by creating a new Macro in Tools/Macro/Macros correct?

  10. #10
    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?

  11. #11
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    MyPSTInbox has point to the Inbox in your local PST, not your default Inbox (the one on the Exchange server).

    [vba]Set MyPSTInbox = GetNS.Folders("Mailbox - Your Name").Folders("Inbox")[/vba]

    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

    [vba]Set MyPSTInbox = GetNS.Folders("Mailbox - Jimmy Pena").Folders("Inbox")[/vba]
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  12. #12
    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

    [vba]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[/vba]

  13. #13
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    Did you redirect your incoming emails to the Exchange account, instead of the local PST?
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  14. #14
    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

  15. #15
    VBAX Expert JP2112's Avatar
    Joined
    Oct 2008
    Location
    Astoria, NY
    Posts
    590
    Location
    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?
    Regards,
    JP

    Read the FAQ
    Getting free help on the web
    My website
    Please use [vba][/vba] tags when posting code

  16. #16
    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!

  17. #17
    Just to keep it alive

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •