INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Outlook: Archive Items by Month Into separate Files

Outlook: Archive Items by Month Into separate Files

(OP)
Outlook has the manual and auto archive options. Auto archive lets you pick one specific file location to archive to. Manually, you can archive whatever you want to whatever folder you want.

Right now, we have a mailbox which I have to check on occasion, and I need to manually archive the past quarter's worth of emails.

What I would like to do is write a VBA script (or something else if more appropriate) to automatically archive "last month's messages" to a file based on the year and month. I'd just say call it YYYYMM for simplicity sake. For example, if I did it today for August, the file would be 201708.pst.

Has anyone done anything like this in Outlook before, and can you offer any starting points or suggestions along the way? I'd want it to run automatically. I'd say only run it the first day of each month, but I think it'd make the most sense to simply run at startup of Outlook or startup plus a few minutes to reduce any load on Outlook.

Thanks for any thoughts.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57

RE: Outlook: Archive Items by Month Into separate Files

Outlook is not my best strength but as no-one has replied to this, I'll try.

I don't have current access to an Exchange server and some of the required code might be slightly different but this works on a stand-alone Outlook system ..

CODE

Sub Archive()
    
    Dim oNameSpace              As Outlook.NameSpace
    
    Dim InboxFolder             As Outlook.MAPIFolder
    Dim PST                     As Outlook.MAPIFolder
    Dim ArchiveFolder           As Outlook.MAPIFolder
    
    Dim DefaultFilePath         As String
    Dim PSTFileName             As String
    Dim ndx                     As Long

    Set oNameSpace = Application.GetNamespace("MAPI")

    ' Find the location of the default Inbox, as an arbitrary location for the new PST
    ' (e.g. "C:\Users\Tony\Documents\Outlook Files")
    With oNameSpace.DefaultStore
        DefaultFilePath = Left(.FilePath, InStrRev(.FilePath, "\"))
    End With
    
    PSTFileName = Format(DateAdd("m", -1, Date), "yyyymm")
    
    ' Create the new PST in the above location with the above name as file name
    ' Note that if the PST already exists, it will be opened and a new one not created
    oNameSpace.AddStoreEx DefaultFilePath & PSTFileName & ".pst", olStoreDefault
    
    ' Access the new PST and name it
    Set PST = oNameSpace.Folders.GetLast
    PST.Name = PSTFileName
    
    ' Create a folder for the archived mail
    ' Note that  this will fail if the folder already exists in an already-existing PST
    ' An error trap or a check for existence may be needed
    Set ArchiveFolder = PST.Folders.Add(PSTFileName)
    
    ' Get a reference to the source Inbox
    ' This is the default Inbox, and will need changing to get a different Inbox
    With oNameSpace.DefaultStore
        Set InboxFolder = .GetDefaultFolder(olFolderInbox)
    End With
    
    ' Move mail items with a date in the month of the file name
    With InboxFolder
        ' When deleting (which MOVing implies), work backward to properly maintain position
        For ndx = .Items.Count To 1 Step -1
            With .Items(ndx)
                If Format(.ReceivedTime, "yyyymm") = PSTFileName Then .Move ArchiveFolder
            End With
        Next ndx
    End With
    
    ' Finally close the new PST
    oNameSpace.RemoveStore PST
    
End Sub 

The next issue is running it. I wouldn't personally be too concerned about system load and running it at start-up and somehow checking for new month would probably work well enough. Outlook doesn't have any very good macro scheduling capacity and if you wanted to automate more cleverly, I would probably suggest creating a monthly appointment and setting a reminder that could be trapped by an event - it's a bit fiddly and, to an extent, non-intuitive, but should work although, as I say, I'm not sure it's worth the effort.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website

RE: Outlook: Archive Items by Month Into separate Files

(OP)
Thanks! I'll give it a look when I can come back to this. Our system is setup as stand-alone units anyway, as we do not have Exchange.

"But thanks be to God, which giveth us the victory through our Lord Jesus Christ." 1 Corinthians 15:57

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close