-2

I have created a folder on root, not under Inbox. What is the syntax to move to root folder?

I'm getting error:

The attempted operation failed

An Object Could not be found

Debug points to the below line myRestrictItems(i).Move myFolder.folders("Business")

Option Explicit

Public Sub Example()

    Dim myNamespace As Outlook.NameSpace
    Dim myFolder As Outlook.MAPIFolder
    Dim myRestrictItems As Outlook.Items
    Dim myItems As Outlook.Items
    Dim myItem As Object
    Dim i As Long
    Dim Filter As String
    Dim Msg As String


    Set myNamespace = Application.GetNamespace("MAPI")
    Set myFolder = myNamespace.GetDefaultFolder(olFolderInbox)
    Set myItems = myFolder.Items

    Filter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:datereceived" & _
                   Chr(34) & " >= '01/01/2018' And " & _
                   Chr(34) & "urn:schemas:httpmail:datereceived" & _
                   Chr(34) & " < '23/03/2018' And " & _
                   Chr(34) & "urn:schemas:httpmail:fromname" & _
                   Chr(34) & "Like '%Jayakumar Krishnamoorthy%'"


    Set myRestrictItems = myItems.Restrict(Filter)

    For i = myRestrictItems.Count To 1 Step -1
        myRestrictItems(i).Move myFolder.folders("Business")

    'Msg = myRestrictItems.Count & " Items in " & myFolder.Name & " Folder"

    'MsgBox (Msg)

    Next

End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

0

Run a quick loop through the folders to debug.print their names and any other properties you might find useful. The displayed name and internal name may not be the same.

AJD
  • 2,400
  • 2
  • 12
  • 22
0

The correct syntax should be


myRestrictItems(i).Move myNamespace.Folders("Business")

Also Move you msgbox outside the loop, Example

Set myRestrictItems = myItems.Restrict(Filter)

Msg = myRestrictItems.Count & " Items in " & myFolder.Name & " Folder, Move it?"

If MsgBox(Msg, vbYesNo) = vbYes Then
    For i = myRestrictItems.Count To 1 Step -1
        myRestrictItems(i).Move myNamespace.Folders("Business")
    Next
End If
0m3r
  • 12,286
  • 15
  • 35
  • 71
  • Thanks 0m3r, Modified as you suggested, the script works like a Champ, I have created a folder under my Inbox and it works fine and successfully moved. What is the syntax if I have to move it to root folder "Business" – Jayakumar krishnamoorthy Mar 23 '18 at 09:30
  • @Jayakumarkrishnamoorthy answer updated with correct syntax – 0m3r Mar 26 '18 at 01:35