Wilson
Wilson

Reputation: 31

Cannot move email to another folder

The line below doesn't work with my Sub. It only shows Add, Application,Class, Count etc. but doesn't really prompt me with the Move function.

objItems.Move objDestFolder

Below are the rest of the code:

Sub MoveEmail()

Dim objNS As NameSpace
Dim objFolder As MAPIFolder
Dim objDestFolder As MAPIFolder
Dim obj As Object
Dim objOL As Outlook.Application
Dim objItems As Outlook.items

    Set objOL = Outlook.Application
    Set objNS = objOL.Application.Session

'specify the Inbox Folder you want to extract the email
Set objFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("For Processing")
Set objItems = objFolder.items

Set objDestFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("For Temp")

For Each obj In objItems
    If obj.Class = olMail Then
        objItems.Move objDestFolder

        Set obj = Nothing
        Set objItems = Nothing
        Set objFolder = Nothing
        Set objOL = Nothing
        Set objDestFolder = Nothing

    End If

Next

End Sub

Upvotes: 0

Views: 996

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66316

You are calling Move on the Items object, not MailItem. You also should not use "for each" if you are modifying the collection. Use a down loop. Change your code to

For i = objItems.Count to 1 step -1
    set obj = objItems.Item(I)
    If obj.Class = olMail Then
        obj.Move objDestFolder
    End If
Next

Upvotes: 1

luke_t
luke_t

Reputation: 2985

Not really sure why you're using a For Each loop, but then setting the collection variable which you're iterating over to nothing, once the object you've iterated to is a Mail Item?

At any rate, I do not believe you can move a whole folder of items at once, hence IntelliSense not providing you with the option to use that Method with the object.

You would need to iterate over each Mail Item within the folder and move them individually.

For Each obj In objItems
    If obj.Class = olMail Then
        obj.Move objDestFolder    
    End If
Next obj

Upvotes: 0

Related Questions