Link to home
Start Free TrialLog in
Avatar of gvmdevelopment
gvmdevelopment

asked on

Outlook 2003 Macro to move messages

Hello,

Hoping to get some help with an outlook 2003 Macro that moves messages from the inbox to another folder under it. I want to be able to move messages from the mailbox inbox to an archive folder which is a .pst on the local hard drive.

I currently use the use the following macro but it only works if the destination folder is under the inbox.


Thanks


 Sub MoveSelectedMessagesToFolder()

On Error Resume Next


    Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder

    Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem


    Set objNS = Application.GetNamespace("MAPI")

    Set objInbox = objNS.GetDefaultFolder(olFolderInbox)

    Set objFolder = objInbox.Folders("_Reviewed")

'Assume this is a mail folder

 
    If objFolder Is Nothing Then

        MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"

    End If
 

    If Application.ActiveExplorer.Selection.Count = 0 Then

        'Require that this procedure be called only when a message is selected

        Exit Sub

    End If


    For Each objItem In Application.ActiveExplorer.Selection

        If objFolder.DefaultItemType = olMailItem Then

            If objItem.Class = olMail Then

                objItem.Move objFolder

            End If

        End If

    Next
 

    Set objItem = Nothing

    Set objFolder = Nothing

    Set objInbox = Nothing

    Set objNS = Nothing

End Sub
ASKER CERTIFIED SOLUTION
Avatar of David Lee
David Lee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ben_stanton
ben_stanton


Can you not simply create a rule in outlook?

Open Outlook then click TOOLS > RULES AND ALERTS then click NEW RULE and follow instructions...

Here is a link with screen grabs; http://pubs.logicalexpressions.com/pub0009/LPMArticle.asp?ID=415
Rules only work on messages as they arrive.  The author stated in their post that this is an archiving operation, so I assume they don't want the messages moved when they arrive.

You can choose when to run the rule. You do not need it running permannntly. As far as I know, you can have it disabled for say 2 months and then run it. It will then ask if you want to run that rule and include what you have in your current specified mailbox. I cannot confirm this as I do not have Outlook in front of me but I am 99% sure.
Yes, that's true.  You can run a rule against items in the inbox.
Avatar of gvmdevelopment

ASKER

Thanks BlueDevilFan,

The macro works great. I needed to be able to select messagse then run a macro which I will attach to a shortcut key. Creating  a rule would not have worked because none of the emails have anything in common.

Sorry, you didn't really spedify that you wanted to move specific emails as opposed to the entire folder. Glad it is working though ;)
I have the same requirement, and unfortunately the Accepted Solution code did not work for me (sorry BlueDevilFan).  I did stumble across a solution however.  The solution below is mainly from my memory; I had found it online 6 months ago, but then had a HDD crash and can not find the source any longer, so I used trial & error to recall it.

The key in gvmdevelopment's code is in the line:
Set objFolder = objInbox.Folders("_Reviewed")

Where "_Reviewed" is the folder nested under the Inbox.

Changing to personal folders can be done by replacing "objInbox" with "objNS", as follows:
Set objFolder = objInbox.Folders("_Reviewed")
Set objFolder = objNS.Folders("_Reviewed")

To go down into the structure, continue adding ".Folders("xxx")"
Set objFolder = objNS.Folders("Level 1").Folders("Level 2").Folders("Level 3").Folders("Level 4")

For example, assume the following folder structure:

Mailbox - Smith, John
  |- Inbox
        |- Inbox_Child_1
  |- Outbox
  |- Sent Items
  |- Deleted Items
Personal Folders
  |- PF_Child_1
  |- PF_Child_2
        |- PF_Grandchild_2A
        |- PF_Grandchild_2B

For each "move to folder" macro (you need one macro per command), alter just the line above to define the target folder; all else can remain the same.

To move to:

Inbox_Child_1 =
Set objFolder = objInbox.Folders("_Reviewed")

Personal Folders =
Set objFolder = objNS.Folders("Personal Folders")

PF_Child_2 =
Set objFolder = objNS.Folders("Personal Folders").Folders("PF_Child_2")

PF_Grandchild_2B =
Set objFolder = objNS.Folders("Personal Folders").Folders("PF_Child_2").Folders(PF_Grandchild_2B")

I have found that this works regardless of where the folder is (could be in Inbox, a nested folder, the Deleted Items folder... anywhere it seems).

The real value in this then is when I assign each macro to a toolbar button and give it an Alt code.  That lets me move the current selected message (whatever I've just viewed/read) to any of the key folders with a single keystroke (like Alt+1).

So, I hope this is helpful.  Please speak up if I've done something wrong and am going to blow up my machine or something (I don't know VB, I just tinker).

Thanks,
Scott