Search code examples
vbaemailoutlookmove

Move Selected Email to Junk E-mail folder in Outlook using VBA Macro


I'm attempting to create a sub that will simply move any currently selected mail to the default junk folder in Outlook when the sub is called.
I'm having a very hard time finding any references I can use, and would appreciate help.


Solution

  • This will work for you

    Sub MoveItems()
        Dim myDestFolder As Outlook.Folder
        Set myDestFolder = Application.GetNamespace("MAPI").Folders("youremailaddress").Folders("[Gmail]").Folders("Spam") ' or Junk
        Dim myItem As Object
        Set myItem = GetCurrentItem
        myItem.Move myDestFolder
    End Sub
    
    Function GetCurrentItem() As Object
        Dim objApp As Outlook.Application
        Set objApp = Application
        On Error Resume Next
        Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
        Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
        Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
        End Select
        GetCurrentItem.UnRead = False
        Set objApp = Nothing
    End Function