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?

[Webinar] Streamline your web hosting managementRegister Today

TWBitConnect With a Mentor Commented:
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)
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.
Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

MarkMichaelConnect With a Mentor Commented:
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.
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?
TWBitConnect With a Mentor Commented:
Don't don't add it to the end, just get rid of it
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.
All Courses

From novice to tech pro — start learning today.