none
Copy a Mail Item to another specificed folder outside of the Inbox RRS feed

  • Question

  • Hi All

    I have found a piece of code that moves a Mail Item to another folder.  However I simply want to "copy", rather than "move" it.  I have tried changed a line from:

             objItem.Move moveToFolder

    to

             objItem.Copy moveToFolder

    but that did not work :-(.  Here is the code I found.  Any help would be appreciated:

    'Outlook VB Macro to move selected mail item(s) to a target folder
    Sub MoveToFiled()
    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("_SSG Support_SSCL.EA.PMO").folders("Two Hour Rule")
    
    If Application.ActiveExplorer.Selection.count = 0 Then
       MsgBox ("No item selected")
       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.Copy moveToFolder
          End If
      End If
    Next
    
    Set objItem = Nothing
    Set moveToFolder = Nothing
    Set ns = Nothing
    
    End Sub
    

    Thanks

    Tony


    TKHussar

    Monday, May 16, 2016 1:05 PM

Answers

  • Use a two step process.

    First, use Copy to create a new instance of the mailitem.  Then use Move to move the new instance to the destination folder.

    For example -

    set copyItem = objItem.Copy

    copyItem.Move moveToFolder


    The use of "set" was quite clear in the earlier suggestion.
    • Marked as answer by TKHussar Tuesday, May 17, 2016 11:01 AM
    Tuesday, May 17, 2016 10:12 AM

All replies

  • Use a two step process.

    First, use Copy to create a new instance of the mailitem.  Then use Move to move the new instance to the destination folder.

    For example -

    set copyItem = objItem.Copy

    copyItem.Move moveToFolder

    Monday, May 16, 2016 1:14 PM
  • Hello,

    The Copy method just creates another instance of an object. You need to call the Move method against the new object (copied). For example, a raw sketch:

    For Each objItem In Application.ActiveExplorer.Selection
       If moveToFolder.DefaultItemType = olMailItem Then
          If objItem.Class = olMail Then
             copiedItem = objItem.Copy
             copiedItem.Move moveToFolder
          End If
      End If
    Next
    


    [custom.development]

    Monday, May 16, 2016 1:31 PM
  • Hi Both

    I have modified the code as suggested above but nothing is being copied to the Two Hour Mails folder!!

    'Outlook VB Macro to move selected mail item(s) to a target folder
    Sub MoveToFiled()
    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("_SSG Support_SSCL.EA.PMO").folders("Two Hour Mails")
    
    If Application.ActiveExplorer.Selection.count = 0 Then
       MsgBox ("No item selected")
       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
             copiedItem = objItem.Copy
             copiedItem.Move moveToFolder
          End If
      End If
    Next
    'For Each objItem In Application.ActiveExplorer.Selection
    '   If moveToFolder.DefaultItemType = olMailItem Then
    '      If objItem.Class = olMail Then
    '         objItem.Copy moveToFolder
    '      End If
    '  End If
    'Next
    
    Set objItem = Nothing
    Set moveToFolder = Nothing
    Set ns = Nothing
    
    End Sub
    

    Please help :-).

    Thanks

    Tony


    TKHussar

    Tuesday, May 17, 2016 7:25 AM
  • Hi Tony,

    You need to set “objItem.Copy” to “copiedItem”. You could try the code below, and pay attention to “Set copiedItem = objItem.Copy”

    For Each objItem In Application.ActiveExplorer.Selection
       If moveToFolder.DefaultItemType = olMailItem Then
          If objItem.Class = olMail Then
             Set copiedItem = objItem.Copy
             copiedItem.Move moveToFolder
          End If
      End If
    Next

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, May 17, 2016 8:10 AM
  • Use a two step process.

    First, use Copy to create a new instance of the mailitem.  Then use Move to move the new instance to the destination folder.

    For example -

    set copyItem = objItem.Copy

    copyItem.Move moveToFolder


    The use of "set" was quite clear in the earlier suggestion.
    • Marked as answer by TKHussar Tuesday, May 17, 2016 11:01 AM
    Tuesday, May 17, 2016 10:12 AM
  • Many thanks both.  It now works like a dream.

    Tony


    TKHussar

    Tuesday, May 17, 2016 11:01 AM