I am trying to create a macro that will move items in my Outlook inbox to a subfolder of another folder that is on the same level as the Inbox (i.e., the parent folder is not a sub-folder of the inbox). This is the code I am using:
Sub EventRequests()
On Error Resume Next
Dim ns As Outlook.NameSpace
Dim moveToFolder As Outlook.MAPIFolder
Dim objItem As Outlook.MailItem
Set ns = Application.GetNamespace("MAPI")
'Define path to the target folder
Set moveToFolder = ns.Folders("Events").Folders("Event Requests")
If Application.ActiveExplorer.Selection.Count = 0 Then
MsgBox ("Select an E-mail first")
Exit Sub
End If
If moveToFolder Is Nothing Then
MsgBox "Target folder not found!", vbOKOnly + vbExclamation, "Move Macro Error"
End If
For Each objItem In Application.ActiveExplorer.Selection
If moveToFolder.DefaultItemType = olMailItem Then
If objItem.Class = olMail Then
objItem.Move moveToFolder
End If
End If
Next
Set objItem = Nothing
Set moveToFolder = Nothing
Set ns = Nothing
End Sub
When I run the code I get an error that says "Target folder not found!" I tried
Set moveToFolder = ns.GetDefaultFolder(olFolderInbox).Folders("Events").Folders("Event Requests")
and Set MoveToFolder = ns.Folders("Mailbox - my name").Folders(targetFolder)
but neither of those worked. I have a different macro set up that moves messages in my inbox to a folder that is a subfolder of my inbox and it works fine:
Set moveToFolder = ns.GetDefaultFolder(olFolderInbox).Folders("Completed")
How do I fix the target path so that it points to the correct subfolder?
Set moveToFolder = ns.GetDefaultFolder(olFolderInbox).Parent.Folders("Events").Folders("Event Requests")
or go directly there withSet MoveToFolder = ns.Folders("The mailbox name not literally Mailbox - my name").Folders("Events").Folders("Event Requests")
– nitonSet moveToFolder = ns.GetDefaultFolder(olFolderInbox).Parent.Folders("Events").Folders("Event Requests")
and it works just fine. – sparkforce