0
votes

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?

1
On Error Resume Next at the start is the worst error in all programming. cpearson.com/Excel/ErrorHandling.htm Move it to just before Set moveToFolder add On Error GoTo 0. Assuming the "Events" folder is directly under the Mailbox. Navigate the folder tree up from the default inbox to Parent then down to the Events folder. Set moveToFolder = ns.GetDefaultFolder(olFolderInbox).Parent.Folders("Events").Folders("Event Requests") or go directly there with Set MoveToFolder = ns.Folders("The mailbox name not literally Mailbox - my name").Folders("Events").Folders("Event Requests")niton
I should have prefaced my comment with "I have no idea what I am doing, I am completely new to this and I'm groping in the dark so I will gladly take any help and instruction anyone is willing to give." I'll read up on the Error handling, thank you for the advice!sparkforce
I'm also working on figuring out the code suggestions you gave me. When I input the first one I get an error that say I need to debug "If moveToFolder.DefaultItemType = olMailItem Then"sparkforce
Nobody has posted an official answer to the target folder question, that would be made invalid by an update. I believe you may update the question with the current code and refocus the title and body to a specific question about the error.niton
I wasn't able to go back and work on this until today and when I ran the new code, everything worked just fine. I didn't change anything but I'm no longer getting the error. I used the first suggestion of Set moveToFolder = ns.GetDefaultFolder(olFolderInbox).Parent.Folders("Events").‌​Folders("Event Requests") and it works just fine.sparkforce

1 Answers

0
votes

I'm going to go ahead and post an answer to my own question so that if other people in the future have this issue they can see the code I arrived at that works:

Sub EventRequests()

Dim ns As Outlook.NameSpace
Dim moveToFolder As Outlook.MAPIFolder
Dim objItem As Outlook.MailItem

Set ns = Application.GetNamespace("MAPI")

On Error GoTo 0
Set moveToFolder = ns.GetDefaultFolder(olFolderInbox).Parent.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