-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
6
  • Do you have subfolder name Business under inbox? Commented Mar 23, 2018 at 7:30
  • I have created folder on root, not under Inbox. Now Created and Tested with 0m3r suggessions, it worked great. What is the syntax to move to root folder "Business" Commented Mar 23, 2018 at 9:28
  • Can you post an image of your outlook folders? Commented Mar 23, 2018 at 9:31
  • Sorry, I could not share the screenshot of the mailbox, as it is my office mailbox. Commented Mar 23, 2018 at 10:40
  • yes, the folder "Business" is in the same level as Inbox. Commented Mar 23, 2018 at 11:01

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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"
@Jayakumarkrishnamoorthy answer updated with correct syntax
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.