VBA - Instead of ActiveExplorer.Selection to set folder, explicitly set folder path

Posted by Mike on Super User See other posts from Super User or by Mike
Published on 2014-06-01T21:27:35Z Indexed on 2014/06/01 21:36 UTC
Read the original article Hit count: 237

Filed under:
|

Sub MoveItems()

Dim Messages As Selection 

Dim Msg As MailItem 

Dim NS As NameSpace 

Set NS = Application.GetNamespace("MAPI") 

Set Messages = ActiveExplorer.Selection 

If Messages.Count = 0 Then

  Exit Sub 

End If 

For Each Msg In Messages 

  Msg.Move NS.Folders("Personal Folders").Folders("SavedMail") 

Next 

End Sub

This code will move all email messages from the currently selected folder in outlook to another folder (SavedMail). I would like to edit the code so that instead of using the currently selected folder as the source for the messages, there would be a hard-coded folder - something like Set Messages = NS.Folders("Personal Folders").Folders("Moved"). I'm a VBA rookie and tried just replacing the Set Messages line with this which resulted in a Run-time error '13': Type mismatch which I think refers to a mismatch of the Dim Messages and the Set Messages commands. I've tried using different Dim definitions with no luck. I'm guessing that someone who knows VBA will see the way to do this right away. Any help would be greatly appreciated. Thanks.

© Super User or respective owner

Related posts about microsoft-outlook

Related posts about vba