Link to home
Start Free TrialLog in
Avatar of Robert Berke
Robert BerkeFlag for United States of America

asked on

vba to move item from junk folder to another folder

I wrote some vba code to check the  Junk mail folder and move specific messages to a different folder.

The code is trivial, but after an hour of trying, it still does not work.

I thought this would be easy but, as usual, it is not.  Outlook 2003 running under windows 7 pro.

The move statement gives this error message

run-time error '-1352531965 (af620003)
Method 'Move' of object 'MailItem' failed

Every time I try it a different error number is returned, but the last 5 hex digits are unchanged.

-1698562045 9ac20003
-1005453309 c4120003
-694026237  dfa20003
-1387134973 ad520003


Option Explicit
Public Sub testJunkMove()

' search junk folder for a specific message and moves it to selected folder.


Dim myFolder As Outlook.MAPIFolder
Dim tgtfolder As Object

Dim i As Long
Dim myolapp As Object, myNameSpace As Object

Set tgtfolder = ActiveExplorer.Selection.Parent
Debug.Print tgtfolder.Caption


Set myolapp = Application
Set myNameSpace = myolapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderJunk)
Dim all2012Subjects As String

    For i = myFolder.Items.Count To 1 Step -1
        Dim item As Object
        Set item = myFolder.Items(i)
        If item.Subject Like "*test*" Then
            MsgBox item.Sent & " " & item.EntryID & " " & item.Subject
            item.Move tgtfolder
        End If
    Next
               
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ScriptAddict
ScriptAddict
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Robert Berke

ASKER

I just gave myself a dope smack to the forehead  !!!

tgtFolder was being set to an Explorer object, when I wanted it to be a MAPIFolder.


Fix#1:      dim tgtFolder As Outlook.MAPIFolder

Fix#2:    Set tgtfolder = ActiveExplorer.Selection.Parent.CurrentFolder

Your reply came after I figured it out, but I am going to give you the points anyhow.  Thanks for your effort.