Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 11-12-2014, 03:31 PM
niton niton is offline Move emails from one folder to another Windows 7 64bit Move emails from one folder to another Office 2010 64bit
Competent Performer
 
Join Date: Jul 2012
Posts: 102
niton is on a distinguished road
Default

To move selected mailitems. No code for searching for items.

Should be valid in 2007 and 2010 (MAPIFolder is 2003, now Folder, but still works)

Code:
 
Sub MoveToFiled()
'On Error Resume Next ' <--- Do not use this unless there is a specific purpose
                      '      Never at the start.
                      '      Not when debugging.
                      '
                      '      Turn it off as quickly as possible with
                      '      On Error GoTo 0
 
Dim ns As Outlook.Namespace
Dim moveToFolder As Outlook.MAPIFolder
 
'Dim objItem As Outlook.mailitem
Dim objItem As Object ' <--- the selected objItem may not be a mailitem
 
Set ns = Application.GetNamespace("MAPI")
 
On Error Resume Next    ' To bypass the error when the target folder is not found.
                        ' moveToFolder will be Nothing
 
' Enter the exact names of the folders
' No slashes. Walk the path one folder at a time.
Set moveToFolder = ns.folders("Personal Folders").folders("Drafts")
On Error GoTo 0         ' No need for On Error Resume Next any more
 
If moveToFolder Is Nothing Then
   MsgBox "Target folder not found!", vbOKOnly + vbExclamation, "Move Macro Error"
 
   ' There will an error moving the selected mail to a folder set to Nothing
   '  since the On Error Resume Next is off
   ' Leave now before this occurs.
   GoTo ExitRoutine
 
Else
   Debug.Print "moveToFolder: " & moveToFolder
End If
 
If Application.ActiveExplorer.Selection.count = 0 Then
   MsgBox ("No item selected")
   ' Exit Sub <--- forgot to clean up
   GoTo ExitRoutine
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
 
ExitRoutine:
    Set objItem = Nothing
    Set moveToFolder = Nothing
    Set ns = Nothing
 
End Sub
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
move emails to folder with a subject that is similar megatronixs Outlook 0 10-14-2014 03:05 AM
Move emails from one folder to another Outlook VBA rule to search email attachements and move the emails into a folder genius7 Outlook 6 09-09-2014 07:01 AM
Move emails from one folder to another Move files from one folder to another obasanla Word 1 09-28-2012 04:42 PM
Move conversation to folder after replying karlads Outlook 0 01-28-2012 12:52 PM
How to move pics in excel to another folder? SPI Excel 1 08-19-2008 11:58 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:44 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft