• Using VBA to move a message to a PST file (Outlook 2003)

    Home » Forums » AskWoody support » Productivity software by function » MS Outlook and email programs » Using VBA to move a message to a PST file (Outlook 2003)

    Author
    Topic
    #451400

    Hi folks. I hope someone can give me a little kickstart with my latest need. I simply want to write a macro that I can use to move a specific message (ostensibly, whatever one is currently selected in my In box) to a folder in PST file stored on my hard-drive. At this point, I must explain that this is for my work machine, which means that my mail messages are (normally) stored on the Exchange server. I mention that because it seemed that a lot of the code I found only handled moving things to a sub-folder. I tried some other code I found elsewhere, but it appears to expect one of Outlook’s “standard” folders, i.e. InBox, Calendar, etc. I don’t know how to direct the code to a seperate PST file/folder.

    Here is the code I tried to use, along with a screen-cap to illustrate the “folder structure” I’m working with.

    Sub MoveEmail()
    Dim myItem As Outlook.MailItem, MyFolder As Outlook.MAPIFolder, olNS As NameSpace

    On Error Resume Next
    Select Case TypeName(Application.ActiveWindow)
    Case “Explorer”
    Set myItem = ActiveExplorer.Selection.Item(1)
    Case “Inspector”
    Set myItem = ActiveInspector.CurrentItem
    Case Else
    End Select
    On Error GoTo 0

    If myItem Is Nothing Then
    MsgBox “Could not use current item. Please select or open a single email.”, vbInformation
    Exit Sub
    End If

    Set olNS = Application.GetNamespace(“MAPI”)
    Set MyFolder = olNS.GetDefaultFolder(olFolderInbox).Folders(“Receipts”)

    myItem.Move MyFolder

    End Sub

    Any help would be greatly appreciated.

    Viewing 2 reply threads
    Author
    Replies
    • #1111235

      You can Move an item from one PST to another simply by creating a Rule for it – provided the target PST is open, usually on Outlook startup. When you set the target Folder for the message in the Rule, you need to browse to the appropriate PST and folder. We’d be happy to help with code, but in this case it would only be necessary if the target PST needs to be opened. (I do a bit of Outlook coding, but I use Rules for this kind of thing.)

    • #1111253

      Rather than use GetDefault___ methods, just use the Folders collection of the namespace. You can check your folder names using this procedure. Make sure to eliminate any duplicates by renaming them in the PST’s Advanced Properties dialog.

      Sub DumpTopLevelFolders()
      Dim ns As Outlook.NameSpace, f As Outlook.MAPIFolder
      Set ns = Application.GetNamespace("MAPI")
      For Each f In ns.Folders
      Debug.Print f.Name
      Next
      Set f = Nothing
      Set ns = Nothing
      End Sub

    • #1111363

      Thank you both John and Jefferson. I am now using the Folders collection to great success; I was unsure what that collection was a member of, nor whether I could use it rather than the GetDefaultFolder approach. (I know next to nothing about the Outlook object model.)

      John, thanks, but I actually wanted to supplement my existing rules with code so that I can quickly move things into specific folders in an ad-hoc fashion. In fact, I am increasingly shying away from rules these days, as I believe I’ve noticed a correlation between the use of rules (of which I currently have about 6) and the noticeable performance hit my machine often takes when Outlook receives mail and runs through all the rules. (At the risk of sounding overly cynical, I seem to have noticed that whenever my machine temporarily stops responding or when it seems to freeze, it almost always coincides with Outlook “receiving” mail from the Exchange server and running the rules I have set up.)

      Again, thanks to you both for the help.

      • #1111377

        A lot of Rules can have a performance impact. There’s some Folder and PSt stuff in this thread that may be of use.

    Viewing 2 reply threads
    Reply To: Using VBA to move a message to a PST file (Outlook 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: