none
Issue moving a folder to another folder RRS feed

  • Question

  • Our IT Dept has implemented an Archive folder on our exchange server mailbox which holds an email for 1 year after its placed in the folder. But if its moved out and then moved back in then that time of course resets. I am wanting to accomplish this with a macro that will move each folder, and subsequently each subfolder and/or their contained mail items,  to my inbox then back into the Archive folder.

    I've tried to use the code below but am getting an 'object required' error on the Folder.MoveTo command:

    Public Sub RefreshArchive()
    On Error GoTo RefreshArchiveErr
    
        Dim objArchive As MAPIFolder
        Dim objMailBox As MAPIFolder
        Dim objInbox As MAPIFolder
        Dim objFolder As MAPIFolder
    
        Set objMailBox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent
    
        Set objInbox = objMailBox.Folders("Inbox") 'Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)
    
        Set objArchive = objMailBox.Folders("Managed Folders").Folders("Archive (1-Year)")
    
         For Each objFolder In objArchive.Folders
            objFolder.MoveTo (objInbox)
        Next
    
        Exit Sub
    
    RefreshArchiveErr:
        MsgBox Err.Number & " " & Err.Description
    End Sub

    I've tried multiple variations on the Destination Folder parameter of the MoveTo function, similar to the coding example in the help file, but always get the object required error.

    Any help would be greatly appreciated.

    PJ


    PJ

    Thursday, July 12, 2012 3:13 AM

Answers

  • You need to loop in reverse as the collection contents are being modified as you remove folders:

    Dim intX As Integer
    
    For intX = objArchive.Folders.Count To 1 Step -1
         Set objFolder = objArchive.Folders.Item(intX)
         objFolder.MoveTo (objInbox)
    Next


    Eric Legault
    MVP (Outlook)
    About me...
    Time-saving Outlook Appins
    for as low as $5!

    • Marked as answer by P'Jorya Friday, July 13, 2012 2:24 AM
    Thursday, July 12, 2012 2:58 PM
    Moderator

All replies

  • Hi PJ,

    Please confirm that objArchive isn't NULL in your code.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us


    Thursday, July 12, 2012 12:33 PM
    Moderator

  • It finds objArchive, because I had a Debug.Print objFolder.Name command prior to the objFolder.MoveTo line in order to see which subFolder under objArchive it would be moving and the Debug.Print objFolder.Name would return the name of a folder under the Archive (1-Year) folder..

    For Each objFolder In objArchive.Folders
         Debug.Print objFolder.Name 'Returns "Personal" which is under Archive (1-Year)
         objFolder.MoveTo (objInbox)
    Next
    I just cleaned up the unnecessary code prior to posting (and the code I did post does still return the error as is)



    PJ


    • Edited by P'Jorya Thursday, July 12, 2012 2:08 PM added more explaination
    Thursday, July 12, 2012 2:06 PM
  • You need to loop in reverse as the collection contents are being modified as you remove folders:

    Dim intX As Integer
    
    For intX = objArchive.Folders.Count To 1 Step -1
         Set objFolder = objArchive.Folders.Item(intX)
         objFolder.MoveTo (objInbox)
    Next


    Eric Legault
    MVP (Outlook)
    About me...
    Time-saving Outlook Appins
    for as low as $5!

    • Marked as answer by P'Jorya Friday, July 13, 2012 2:24 AM
    Thursday, July 12, 2012 2:58 PM
    Moderator
  • You would only get that exception if either the objFolder object is null or your Inbox folder reference is null. Are you sure that's not the case?
     
    Why are you getting Inbox in that roundabout way? Why not use GetDefaultFolder(olFolderInbox) as you have commented out?

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "P'Jorya" <=?utf-8?B?UCdKb3J5YQ==?=> wrote in message news:79498db8-9bac-40d6-b83b-5e35f7247669...


    It finds objArchive, because I had a Debug.Print objFolder.Name command prior to the objFolder.MoveTo line in order to see which subFolder under objArchive it would be moving and the Debug.Print objFolder.Name would return the name of a folder under the Archive (1-Year) folder..

    For Each objFolder In objArchive.Folders
         Debug.Print objFolder.Name 'Returns "Personal" which is under Archive (1-Year)
         objFolder.MoveTo (objInbox)
    Next
    I just cleaned up the unnecessary code prior to posting (and the code I did post does still return the error as is)



    PJ



    Ken Slovak MVP - Outlook
    Thursday, July 12, 2012 3:06 PM
    Moderator
  • OMG! Its a slap my forehead moment.

    Eric: I tried your code as posted and still got an error on the objFolder.MoveTo (objInbox) line, but you are right I need to do a reverse loop because the contents were being moved out so I marked it as the answer though in and of itself it did not fix my error.

    Ken: neither were null and I used various ways for everything in an attempt to figure out what might be causing my error.

    When I went back and looked at the help file it hit me.. the use of the () around objInbox for the MoveTo function would cause an object to be required to the left of the whole line, such as in:

    vbYes = Msgbox ("Am I really that blonde?")

    as opposed to:

    Msgbox "Hopefully, I didn't waste everyone's time."

    which doesn't require an assignment object...

    I simply removed the () and the code worked fine..As I said a slap my forehead moment.

    Thank you all soo much for your help and responses!


    PJ


    • Edited by P'Jorya Friday, July 13, 2012 2:34 AM
    Friday, July 13, 2012 2:32 AM
  • Thanks, I was looking for how to programatically move an Outlook 2003 folder and this worked fine - after removing the parenthesis.
    Tuesday, November 12, 2013 1:50 PM