Outlook Macro to Move messages between folders

I am trying to follow the advise from:


I want to create some macros that will allow me to move Outlook 2007 messages between folders.

I have zero experience with VB and am having trouble getting this to work. I have pasted the following code from the above post into VB - Module 1:

I do not understand what it means to "call the script and pass it something."

I have tried adding:

Sub MoveToReportsFolder()
        MoveMessages "\Personal Folders\Reports"
    End Sub

To the end of the script but it keeps giving me:

Compile Error: Invalid Outside procedure.

Any thoughts on what i might be doing wrong?
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.

ChristarAuthor Commented:

    MoveMessages "\Folder\SubFolder"
Sub MoveMessages(strFolder As String)
    Dim olkItem As Object, _
        olkFolder As Outlook.MAPIFolder
    Set olkFolder = OpenMAPIFolder(strFolder)
    If TypeName(olkFolder) = "MAPIFolder" Then
        For Each olkItem In Application.ActiveExplorer.Selection
            olkItem.UnRead = False
            olkItem.Move olkFolder
    End If
    Set olkFolder = Nothing
    Set olkItem = Nothing
End Sub
'Credit where credit is due.
'The code below is not mine.  I found it somewhere on the internet but do
'not remember where or who the author is.  The original author(s) deserves all
'the credit for these functions.
Function OpenMAPIFolder(szPath)
    Dim app, ns, flr, szDir, i
    Set flr = Nothing
    Set app = CreateObject("Outlook.Application")
    If Left(szPath, Len("\")) = "\" Then
        szPath = Mid(szPath, Len("\") + 1)
        Set flr = app.ActiveExplorer.CurrentFolder
    End If
    While szPath <> ""
        i = InStr(szPath, "\")
        If i Then
            szDir = Left(szPath, i - 1)
            szPath = Mid(szPath, i + Len("\"))
            szDir = szPath
            szPath = ""
        End If
        If IsNothing(flr) Then
            Set ns = app.GetNamespace("MAPI")
            Set flr = ns.Folders(szDir)
            Set flr = flr.Folders(szDir)
        End If
    Set OpenMAPIFolder = flr
End Function
Function IsNothing(obj)
  If TypeName(obj) = "Nothing" Then
    IsNothing = True
    IsNothing = False
  End If
End Function

Open in new window

>> I have tried adding:

>> Sub MoveToReportsFolder()
>>        MoveMessages "\Personal Folders\Reports"
>> End Sub

This is correct.  Get rid of it on Line 1.
Take out the top line of "MoveMessages "\Folder\SubFolder"
and save it.

Close the VB editor
Click Tools > Macro > Macros > type a name u want to call it here. eg..
Click Create
Then VB Editor will open and you can put the folder command in there.

Sub MoveToDrafts()
   MoveMessages "\Inbox\Drafts"
End Sub

This will then call the above code.
and will move selected messages to the folder you have placed here.
You can create lots of macros with different locations and drag these macros onto your toolbar at the top.
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

ChristarAuthor Commented:
I have removed MoveMessages "\Folder\SubFolder" in line 1 of the above code and adding it to the end. Is this what you mean?

It is now giving me:

Runtime error '-2147221233 (8004010f)':
The operation failed. An object could not be found.

When i click on Debug it highlights Line :  Set flr = ns.Folders(szDir)

Does this mean that I have my destination folder defined incorrectly?
Don't don't add it to the end, just get rid of it
What is the exact value you specified for the folder?  Are you still using "\Personal Folders\Reports"?  This is okay as long as you are working in your set of Personal Folders, and there is a Reports folder in the root (not nested inside any other folder)

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
ChristarAuthor Commented:
Sorry I am still slightly confused. Where do i add the part?:

Sub MoveToDrafts()
   MoveMessages "\Inbox\Drafts"
End Sub

When the VB Editor opens after clicking 'Create' Macro, where to i put the folder command? Do i create a new module?

When VB Editor opens back up it has added a bit at the end:

Sub MoveToDrafts()

End Sub

Do i simply insert 'MoveMessages "\Inbox\Drafts"' in the middle of this?
ChristarAuthor Commented:
The exact value i specified was "\Inbox\Faxes' as i have a folder labeled as such in my inbox...
ChristarAuthor Commented:
I had specified the the value in incorrectly when pointing to the folder

I had to use:

'\\Chris Outlook PST\Inbox\Faxes'

Thanks for your help TWBi & MarkMichael.
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
VB Script

From novice to tech pro — start learning today.