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

LVL 5
rberkeConsultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ScriptAddictCommented:
So,

If this was a one time event I would just search for it.  Or if it was reoccuring I'd just create a rule in outlook to make the move.  

However if your set on using VBA here is how I would do it:

Helpful Website

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rberkeConsultantAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Outlook

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.