Outlook VBA : Move email to a different folder (1 Viewer)

Derek

Registered User.
Local time
Today, 00:42
Joined
May 4, 2010
Messages
234
Hi Guys

I have written following code and it works fine if the 'Completed' folder is within My Inbox (Personnel folder) but in my outlook there are some other group folders as well so if I create 'Completed' folder within the group folder then the below code doesn't work. So I am looking for VBA code that will check the parent folder and if its 'My inbox' then move the email from there to 'Completed' folder within that.

And if the parent folder is different then move selected email to 'Completed' folder within that.

Code:
Sub ProcessSelection()
Dim olMailItem As Object
      If Application.ActiveExplorer.Selection.Count = 0 Then
        MsgBox "No Items selected!", vbCritical, "Error"
        Exit Sub
    End If
    On Error Resume Next
    For Each olMailItem In Application.ActiveExplorer.Selection
             SaveAttachments olMailItem
           DoEvents
    Next olMailItem
Err_Handler:
    Set olMailItem = Nothing
lbl_Exit:
    Exit Sub
End Sub
Code:
Private Sub SaveAttachments(olItem As Object)
Set myNameSpace = Application.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myItems = myInbox.Items
Set myDestFolder = myInbox.Folders("Completed")
olItem.Move myDestFolder
Set olItem = myItems.FindNext
olItem.Move myDestFolder
Set olItem = myItems.FindNext
 
Last edited:

Cronk

Registered User.
Local time
Today, 17:42
Joined
Jul 4, 2013
Messages
2,772
If you know the name of the group folder containing the "Completed" folder then
Code:
Set myDestFolder = myNameSpace.Folders("yourFolderName").Folders("Completed")
If you don't know which group folder contains the Completed folder, you will have to loop through all the folders searching for it.
 

Derek

Registered User.
Local time
Today, 00:42
Joined
May 4, 2010
Messages
234
I am looking for a way to find out the folder name where the selected email is and then move that selected item to the 'Completed' Folder within that folder. If there are 2 mail box folders which have 'Completed' folders (so same name of the subfolders within 2 mailboxes) then there should be way to identify the parent folder name and move the selected email to that Completed folder.

Hope it makes sense. Many Thanks
 

Derek

Registered User.
Local time
Today, 00:42
Joined
May 4, 2010
Messages
234
I have written the following piece of code and it works great :
Code:
Private Sub SaveAttachments(olItem As Object)
Dim myParentFolder As Folder
Dim myDestFolder As Outlook.Folder
Set myParentFolder = olItem.Parent

If myParentFolder = "Inbox" Then
        
        Set myDestFolder = myParentFolder.Folders("Completed")
        olItem.Move myDestFolder
        
Else
       
        Set myParentFolder = myParentFolder.Parent
                
End If
End sub
 

Users who are viewing this thread

Top Bottom