+ Reply to Thread
Results 1 to 11 of 11

VBA Macro to Move Aged Email Messages To Folder

  1. #1
    Registered User
    Join Date
    12-31-2010
    Location
    Kathleen, Georgia
    MS-Off Ver
    Excel 2003
    Posts
    27

    VBA Macro to Move Aged Email Messages To Folder

    Hi All, if anyone can provide help I would greatly appreciate it. I found some vb online to do exactly what I wanted to do, but I can't get it to work. I'm losing tons of valuable emails due to the companies email retention policy. Currently spending too much time trying to find these emails before they age out and move them to my Mailstore program.

    I pasted this code into a new module and also into ThisOutlookSession and neither works.

    Thanks
    Jason


    Sub MoveAgedMail()

    Dim objOutlook As Outlook.Application
    Dim objNamespace As Outlook.NameSpace
    Dim objSourceFolder As Outlook.MAPIFolder
    Dim objDestFolder As Outlook.MAPIFolder
    Dim objVariant As Variant
    Dim lngMovedItems As Long
    Dim intCount As Integer
    Dim intDateDiff As Integer
    Dim strDestFolder As String

    Set objOutlook = Application
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    Set objSourceFolder = objNamespace.GetDefaultFolder(olFolderInbox)

    ' use a subfolder under Inbox
    Set objDestFolder = objSourceFolder.Folders("01_Aged")

    For intCount = objSourceFolder.Items.Count To 1 Step -1
    Set objVariant = objSourceFolder.Items.Item(intCount)
    DoEvents
    If objVariant.Class = olMail Then

    intDateDiff = DateDiff("d", objVariant.ReceivedTime, Now)

    ' I'm using 7 days, adjust as needed.
    If intDateDiff > 300 Then

    objVariant.Move objDestFolder

    'count the # of items moved
    lngMovedItems = lngMovedItems + 1

    End If
    End If
    Next

    ' Display the number of items that were moved.
    MsgBox "Moved " & lngMovedItems & " messages(s)."
    Set objDestFolder = Nothing
    End Sub
    Attached Images Attached Images

  2. #2
    Forum Contributor
    Join Date
    07-24-2023
    Location
    Como, Italy
    MS-Off Ver
    MSO 365 - Ver 2208
    Posts
    120

    Re: VBA Macro to Move Aged Email Messages To Folder

    Hi jasono,

    I confirm that the code should be put in a standard module.

    I also tried the Sub and it correctly moves the emails, which are at least 300gg old from one folder to another.

    In the first analysis I would like to ask you to check:.
    • the presence of the source and destination folders (you said it is an inbox subfolder);
    • the presence of emails within the source folder;
    • the possession of the privileges to be able to manage the email.

    You can also try, with VBE, to follow step by step with F8 the procedure to trace the code and check the contents of the variables. If the result was different from the expected one, it would be worth further investigation.

    Let us know the outcome of the investigations.

    Good luck
    Max
    let's compare ideas

  3. #3
    Registered User
    Join Date
    12-31-2010
    Location
    Kathleen, Georgia
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: VBA Macro to Move Aged Email Messages To Folder

    Thank you for the response. So this does work, only when I have the aged email in my Inbox folder. Code will move the aged email from the Inbox to my sub folder. However all of my saved emails are in subfolders of my Inbox. Any idea on how the code could search my Inbox subfolders? Thanks

  4. #4
    Forum Contributor
    Join Date
    07-24-2023
    Location
    Como, Italy
    MS-Off Ver
    MSO 365 - Ver 2208
    Posts
    120

    Re: VBA Macro to Move Aged Email Messages To Folder

    Hi jasono,

    if you want to take email from one inbox subfolder and save them to another inbox subfolder you should use this code to identify the folders:
    Please Login or Register  to view this content.
    after this part you should replace all subsequent objSourceFolder with objSourceSubFolder

    Try and let us know.

    Bye
    Last edited by maxpit; 09-15-2023 at 01:19 AM.

  5. #5
    Registered User
    Join Date
    12-31-2010
    Location
    Kathleen, Georgia
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: VBA Macro to Move Aged Email Messages To Folder

    Sorry...left out an important detail here. I have over a hundred sub folders. Each sub folder is an active project I'm managing. Is there a way to have the code scan all folders without having to name each one? Thanks, Jason

  6. #6
    Forum Contributor
    Join Date
    07-24-2023
    Location
    Como, Italy
    MS-Off Ver
    MSO 365 - Ver 2208
    Posts
    120

    Re: VBA Macro to Move Aged Email Messages To Folder

    Hi jasono,

    with the Sub below, you can get the list of folders, the number of items and the number of sub-folders by passing the 'Store' reference as a parameter:
    Please Login or Register  to view this content.
    The function is called, for example, with:
    Please Login or Register  to view this content.
    Remember that you may have opened more than one 'Store', so in the move phase it will have to be evaluated.

    See you soon
    Last edited by maxpit; 09-16-2023 at 01:21 PM.

  7. #7
    Registered User
    Join Date
    12-31-2010
    Location
    Kathleen, Georgia
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: VBA Macro to Move Aged Email Messages To Folder

    But won't this just give me a list of my subfolders? Then I would have to go in and name all of these folders in the MoveAgedMail routine? If that were the case I might have to rethink what I was attempting to do. Thanks for the guidance.

  8. #8
    Forum Contributor
    Join Date
    07-24-2023
    Location
    Como, Italy
    MS-Off Ver
    MSO 365 - Ver 2208
    Posts
    120

    Re: VBA Macro to Move Aged Email Messages To Folder

    Hi jasono,


    The pointers I have given you serve the purpose, but it is important to have in mind the whole path to the resolution of the problem.

    Look deeper into the problem: are there only folders under the main one, or are there several so that a recursive function must be provided?

    Then where should they be copied: I think the contents should be in a file like ".pst" and with what structure?

    Go deeper and acquire as many details as possible: if appropriate break the big problem into many smaller, more manageable parts.

    Let us know if you would like to continue with more details.

    Bye



    Another Info:
    Last edited by maxpit; 09-17-2023 at 11:58 AM.

  9. #9
    Forum Contributor
    Join Date
    07-24-2023
    Location
    Como, Italy
    MS-Off Ver
    MSO 365 - Ver 2208
    Posts
    120

    Re: VBA Macro to Move Aged Email Messages To Folder

    Hi jasono,

    To help you with the resolution, I have modified the entire code so that it is recursive and can go and read the entire store indicated.

    It returns, like the previous one, the following information in the Result() array:
    • the name of the folder;
    • the number of objects present;
    • the number of sub-folders present.
    Please Login or Register  to view this content.
    I invite you to perform a test and give feedback.

    See you soon
    Last edited by maxpit; 09-17-2023 at 04:26 PM.

  10. #10
    Registered User
    Join Date
    12-31-2010
    Location
    Kathleen, Georgia
    MS-Off Ver
    Excel 2003
    Posts
    27

    Re: VBA Macro to Move Aged Email Messages To Folder

    Well I copied the code and pasted the Test routine into a module. Nothing happens when I try and run it. Sorry wish I was more skilled at this..

  11. #11
    Forum Contributor
    Join Date
    07-24-2023
    Location
    Como, Italy
    MS-Off Ver
    MSO 365 - Ver 2208
    Posts
    120

    Re: VBA Macro to Move Aged Email Messages To Folder

    Hi jasono,

    Array Result(0 to 2, ...) contains all the data indicated: folder name, number of object (email, ...) and number of sub-folders

    Replace sub Test with Test1 while Sub ListSubFolder is unmodified:
    Please Login or Register  to view this content.
    you will get collected data in a worksheet for each folder in MAPI-namespace: folder name, number of objects (email) and number of sub-folders.

    Attention: you will have to add Reference to Microsoft Excel library in Outlook: Tools -> References

    Ref VBA in Outlook for Excel.JPG

    Also try this and let us know.

    Bye
    Last edited by maxpit; 09-20-2023 at 12:36 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to move an email from sent folder to another sent folder
    By taylorsm in forum Outlook Formatting & Functions
    Replies: 0
    Last Post: 06-06-2017, 12:56 PM
  2. Replies: 6
    Last Post: 11-24-2015, 01:32 PM
  3. look for text in email body and move email to a folder
    By Megatronixs in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-10-2015, 05:23 AM
  4. Move an email to a folder in outlook using macro
    By Arunkumark in forum Outlook Formatting & Functions
    Replies: 0
    Last Post: 07-11-2013, 09:33 AM
  5. Text box to move messages to a specific folder
    By Interex in forum Outlook Programming / VBA / Macros
    Replies: 0
    Last Post: 04-24-2012, 05:39 PM
  6. Outlook 2007 rule to move "marked as complete" messages to a specified folder
    By cybnexcel in forum Outlook Formatting & Functions
    Replies: 0
    Last Post: 03-24-2011, 11:28 AM
  7. unable to move email to a folder
    By Ranbir in forum Outlook Formatting & Functions
    Replies: 0
    Last Post: 03-03-2010, 08:18 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1