Help with a Macro to move emails to a different PST data file

Discussion in 'Using Outlook' started by CodeVar, Nov 1, 2013.

  1. CodeVar

    CodeVar

    Member
    I was hoping someone could help me understand how to move emails to a different PST file. Currently I have edited a script I found online which allows emails to be moved to various folders based on a macro which executes either from a button in the top menu bar area, or with shortcut keys. Right now it is all working excellent, but I want to take it a step further and have it function the same but also allow a button which will move emails to a separate PST file. This particular macro will be running in Outlook 2007.

    I read this but I wasn't able to get it functioning based on the macro I have customized:

    http://www.slipstick.com/developer/macro-move-aged-mail/

    My current Macro is as follows:

    -It allows emails to be moved to 3 different folder locations within the main PST "Inbox" folder.

    MACRO:

    ----------------------------------- 'Outlook VB Macro to move selected mail item(s) to a target folder

    Sub MoveToFolder(targetFolder)

    On Error Resume Next

    Dim ns As Outlook.NameSpace

    Dim MoveToFolder As Outlook.MAPIFolder

    Dim objItem As Outlook.MailItem

    Set ns = Application.GetNamespace("MAPI")

    'define path to the target folder; the following assumes the target folder

    'is a sub-folder of the main Mailbox folder

    'This is the original'

    'Set MoveToFolder = ns.Folders("Mailbox").Folders(targetFolder)'

    Set MoveToFolder = ns.GetDefaultFolder(olFolderInbox).Folders(targetFolder)

    If Application.ActiveExplorer.Selection.Count = 0 Then

    MsgBox ("No item selected")

    Exit Sub

    End If

    If MoveToFolder Is Nothing Then

    MsgBox "Target folder not found!", vbOKOnly + vbExclamation, "Move Macro Error"

    End If

    For Each objItem In Application.ActiveExplorer.Selection

    If MoveToFolder.DefaultItemType = olMailItem Then

    If objItem.Class = olMail Then

    objItem.Move MoveToFolder

    End If

    End If

    Next

    Set objItem = Nothing

    Set MoveToFolder = Nothing

    Set ns = Nothing

    End Sub

    Sub MoveToActive()

    MoveToFolder ("Active")

    End Sub

    Sub MoveToAction()

    MoveToFolder ("Action")

    End Sub

    Sub MoveToOnHold()

    MoveToFolder ("OnHold")

    End Sub

    -----------------------------------Can someone help me configure it so a 4th option will move an email to a folder within a different PST?

    For example I would like to add an extra button called "Archive", and when this particular button is clicked it will move the email to the archive folder within the separate PST's Inbox

    Sub MoveToArchive()

    MoveToFolder ("Archive")

    End Sub
     
  2. Diane Poremsky

    Diane Poremsky

    Senior Member
    You need to get the folder path function - http://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/




    then call the path like this:




    Set MoveToFolder = GetFolderPath(targetF older)




    and call it using



    MoveToFolder ("datafile-name-in-folderlist\Inbox\subfoldername")








    I think it will work with the default folders too, so you could use one macro and the full path to the file in GetFolderPath - or you can use two macros, your orginial and one that uses the getfolderpath function.
     
  3. CodeVar

    CodeVar

    Member
    I want to start by saying Thank You, this was helpful in understanding the code further, but I'm still not able to get it to function. I tried to make a bunch of edits and possible adjustments but I don't know VBA well enough to make it work after all.

    I'm going to try a bunch of different scenario's and will post back if I get it or not. If not, would you be able to possibly tweak the entire body of code so I can copy/paste it if I share the exact structure? I'll let you know how I make out on my own for now.
     
  4. Diane Poremsky

    Diane Poremsky

    Senior Member
    Yeah, if you can't get it working, I'll try it.
     
  5. CodeVar

    CodeVar

    Member
    So I tried a bunch of combinations, but with no success. First using the original scrip, as well as the authors earlier version but couldn't get it to work as hoped with this extra functionality.

    I am using this as a base with minor edits for my needs and to move emails to folders within the main data pst inbox folder (Look at the code which is stated as being updated 5/25/2011 which is more compact, but unsure if may be causing issues when trying to amend this new function):

    http://jmerrell.com/2011/05/21/outlook-macros-move-email/

    My goal is to keep this functioning as is, but then also add the ability to move an email to a separate pst archive folder. This new button I am looking to create will move an email to the secondary Archive PST in folder 1, or folder 2, both of which reside within the archive Inbox folder. I run both of these pst's in the same profile. My current example structure is a follows:

    Main PST

    Inbox

    -Folder 1

    -Folder 2

    Secondary PST/Archive

    Inbox

    -Folder 1

    -Folder 2

    Any extra help you can share will be appreciated :)
     
Loading...

Share This Page