none
Moving into Folders. RRS feed

  • Question

  • Can someone please assist in revising this code to work on emails labeled with a specific category, for example "Johny".<o:p></o:p>

    I have a macro that labels the emails with category "Johny", and would be great if I could move them into folder "JohnysEmails" without selecting each one inside the box.<o:p></o:p>

     

    Thank you very much!<o:p></o:p>

    Sub AutoForwardMove()
    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("Mailbox - Jim Merrell").Folders("@Filed")


    Set moveToFolder = ns.Folders("Mailbox - Vlad").Folders("Johny")


    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.Move moveToFolder
          End If
      End If
    Next

    Set objItem = Nothing
    Set moveToFolder = Nothing
    Set ns = Nothing

    End Sub

    Tuesday, March 22, 2016 7:25 PM

Answers

  • Hello Vlad,

    For Each objItem In Application.ActiveExplorer.Selection If moveToFolder.DefaultItemType = olMailItem Then If objItem.Class = olMail Then

    If objItem.Categories = "Johny" Then
    objItem.Move moveToFolder

    End If End If End If Next


    The Categories property returns a string representing the categories assigned to the Outlook item. Categories is a delimited string of category names that have been assigned to an Outlook item. This property uses the character specified in the value name, sList, under HKEY_CURRENT_USER\Control Panel\International in the Windows registry, as the delimiter for multiple categories. To convert the string of category names to an array of category names, use the Microsoft Visual Basic function Split.

    Wednesday, March 23, 2016 11:56 AM

All replies

  • Hi, VladSNY

    >>First I want to confirm with you that had you try to run above mentioned code? is it moving your selected mails in to johny Folder? 

    because on my side I am getting error that's why I want to confirm with you first?

    Regards

    Deepak


    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.

    Wednesday, March 23, 2016 6:31 AM
    Moderator
  • Hi VladSNY,

    This does not sound hard,

    Dim inboxFolder As Outlook.MAPIFolder
    Set inboxFolder = ns.Folders("Mailbox - Vlad").Folders("Inbox")
    For Each objItem In inboxFolder.Items
        If objItem.Categories = "Johny" Then
          If objItem.Class = olMail Then
             objItem.Move moveToFolder
          End If
        End If
    Next

    Wednesday, March 23, 2016 7:20 AM
  • Jackie,

    Please stop posting inaccurate sample code. Iterating through all items in the folder is not really a good idea.

    If you need to get a subset of items from a folder you need to use the Find/FindNext or Restrict methods of the Items class. Read more about these methods in the following articles:

    How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)

    How To: Use Restrict method to retrieve Outlook mail items from a folder

    Wednesday, March 23, 2016 11:50 AM
  • Hello Vlad,

    For Each objItem In Application.ActiveExplorer.Selection If moveToFolder.DefaultItemType = olMailItem Then If objItem.Class = olMail Then

    If objItem.Categories = "Johny" Then
    objItem.Move moveToFolder

    End If End If End If Next


    The Categories property returns a string representing the categories assigned to the Outlook item. Categories is a delimited string of category names that have been assigned to an Outlook item. This property uses the character specified in the value name, sList, under HKEY_CURRENT_USER\Control Panel\International in the Windows registry, as the delimiter for multiple categories. To convert the string of category names to an array of category names, use the Microsoft Visual Basic function Split.

    Wednesday, March 23, 2016 11:56 AM