Results 1 to 5 of 5
  1. #1
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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("Rece ipts")

    myItem.Move MyFolder

    End Sub

    Any help would be greatly appreciated.
    Attached Images Attached Images
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  2. #2
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

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

    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.)
    -John ... I float in liquid gardens
    UTC -7ąDS

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Using VBA to move a message to a PST file (Out

    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.

    <code>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</code>

  4. #4
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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.
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  5. #5
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

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

    A lot of Rules can have a performance impact. There's some Folder and PSt stuff in <!post=this thread,709,604>this thread<!/post> that may be of use.
    -John ... I float in liquid gardens
    UTC -7ąDS

Posting Permissions

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