Outlook 2007 move mail to specific folder VB macro

larspanky
larspanky used Ask the Experts™
on
I am working out a macro that will move items to a folder called
"~Filtered Spam"

This folder is the top folder of all the users in exchange.  I used the macro below to move TO the inbox and now I need it from whatever folder TO the ~Filtered Spam folder.  This is part of another app for allowing users to move SPAM or False Positives.  

All that should be required is the relative path to this folder.
Changing 'Set objFolder = objNS.GetDefaultFolder(olFolderInbox)'
to 'Set objFolder = objNS.Folders("~Filtered Spam")

Or something like that, I cant find how to reference the path.

Thanks
Sub MoveToInbox()
On Error Resume Next
    Dim objFolder As Outlook.MAPIFolder
    Dim objNS As Outlook.NameSpace, objItem As Outlook.mailItem
    Set objNS = Application.GetNamespace("MAPI")
    Set objFolder = objNS.GetDefaultFolder(olFolderInbox)
'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.UnRead = False
                objItem.Move objFolder
            End If
        End If
    Next
 
    Set objItem = Nothing
    Set objFolder = Nothing
    Set objNS = Nothing
End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
see http://www.outlookcode.com/d/code/getfolder.htm for a OL VBA method of GetFolder

Set objFolder =  GetFolder("Personal Folders\~Filtered Spam")

...your script

Author

Commented:
I am not sure this method will work for me.  Its not in a personal folder.  Its in the mailbox, same tree is the Mailbox in box.   GetDefaultFolder(olFolderInbox)

But since my folder is not a default folder there is no object for it.  

Is it actually Personal Folders\~Filtered Spam, even though its in the mailbox tree?

Thanks

Author

Commented:
This is what I am trying now.  
http://stackoverflow.com/questions/2235745/get-mailbox-name-in-outlook-addin-using-vsto

I can't get this to work either.  Its not setting objFolder to ~Filtered Spam.

Set objFolder = objNS.myParentFolder.Folders("~Filtered Spam")

Any thoughts?

Thanks
Sub MoveToSPAM()
 
On Error Resume Next
    Dim objFolder As Outlook.MAPIFolder
    Dim objNS As Outlook.NameSpace, objItem As Outlook.mailItem, myFolder As Outlook.Folder
    Set objNS = Application.GetNamespace("MAPI")
    Set myNameSpace = objNS
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
    Set myParentFolder = myFolder.Parent
    Set objFolder = objNS.myParentFolder.Folders("~Filtered Spam")
'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
        
    End If
 
    For Each objItem In Application.ActiveExplorer.Selection
        If objFolder.DefaultItemType = olMailItem Then
            If objItem.Class = olMail Then
                objItem.UnRead = False
                objItem.Move objFolder
            End If
        End If
    Next
 
    Set objItem = Nothing
    Set objFolder = Nothing
    Set objNS = Nothing
   
End Sub

Open in new window

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

1/ I believe that Personal Folders is the mailbox tree.  so Inbox is a subfolder of Personal Folders.  you can see this on the code from Outlook.com

2/ but your code to get parent and then the subfolder should work also

3/ can you get the code to loop through subfolders using msgbox or similar to see what outlook believes are the subfolders ?

4/ are you running this from a form script (VBS) or from VBA in another app ?

Author

Commented:
Its running from a button in outlook, its just a VBS macro. I tried to get a output to a message box and its not even finding the inbox.   Let me know if you have any other ideas.

Thanks
only minor changes needed for me on OL2007  see the <<<<

I changed the MapiFolder to Folder; not necessasry but better supported ?

objNS.myParentFolder.Folders  is incorrect.  needs to be just   myParentFolder.Folders

should turn off On Error Resume when debugging.   comment out my Found msgbox when happy.


macro seems to work ok after that.

Sub MoveToSPAM()
 
'On Error Resume Next
    Dim objFolder As Outlook.Folder   '<<<<<
    Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem, myFolder As Outlook.Folder
    Set objNS = Application.GetNamespace("MAPI")
    'MsgBox "ons=" & objNS.CurrentUser
    
    Set myNameSpace = objNS
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
    'MsgBox "myf=" & myFolder.Name
    
    Set myParentFolder = myFolder.Parent
    'MsgBox "mypf=" & myParentFolder.Name & "...count=" & myParentFolder.Folders.Count
    
    Set objFolder = myParentFolder.Folders.Item("~Filtered Spam")  '<<<<<<
    

'Assume this is a mail folder
 
 
    If objFolder Is Nothing Then
        MsgBox "This folder doesn’t exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
     Else
        MsgBox "Found :" & objFolder.Name & "=" & objFolder.Description
    End If

Open in new window

Author

Commented:
It works great,

Thank You

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial