PDA

View Full Version : Outlook VBA - Move Mail - Exchange Folder



longestdrive
01-07-2011, 03:44 AM
Hi

I'm dabbling in outlook 2003 vba and I'm struggling with the folders which might be my biggest problem.

I found some basic code to move selected items to a folder and thought I'd try this. However when I run my code I get an error '424 - object required' which I assume means it can't find the folder I'm trying to set as my destination.

Here's the latest version of my code that's causing the error:



SubMoveItems()
Set myOlApp = CreateObject("Outlook.Application")
Dim Messages As Selection
Dim Msg As MailItem
Dim myNamSpace As NameSpace
Dim Proceed As VbMsgBoxResult
Dim myFolder As Outlook.MAPIFolder

Set myNamSpace = myOlApp.GetNamespace("MAPI")
Set Messages = ActiveExplorer.Selection
'Set myFolder = myNameSpace.Folders("GTD").Folders("Planning")


Debug.Print ("Mess:" & Messages.Count)
If Messages.Count = 0 Then
Exit Sub
End If
For Each Msg In Messages
'If Msg.FlagStatus = 2 Then
Proceed = MsgBox("Are you sure you want to clear the flag and move the message " _
& """" & Msg & """" & " to the folder " & """" & "Planning" & """" & "?", _
vbYesNo + vbQuestion, "Confirm Procedure")
If Proceed = vbYes Then
Msg.FlagStatus = olNoFlag
Msg.Move myNamSpace.Folders("GTD").Folders("Planning")
End If
'End If
Next

End Sub




Note I've commented out the set myfolder bit on purpose as I thought i'd create the object first but still comes up with the error at the SET statement instead.

For info the 'GTD' folder is at the root of my exchange mailbox, it's not a subfolder of inbox and its not in a personal folder. The folder 'Planning' is a subfolder of 'GTD'

I'm not convinced I have referenced the 'GTD' folder correctly.

Can anyone point me in the right direction please

I've commented out the if statement relating to flag status for now until I fix this problem

Thank you

gcomyn
01-21-2011, 01:28 PM
I also have been dabbling with outlook coding, and I have folders that are on the same level as Inbox (not subfolders of it), and this is what I use to set the 'Parent' folder...


Set myFolder = myNamSpace.GetDefaultFolder(olFolderInbox).Parent.Folders("GTD").Folders("Planning")

(I used your variables from above)...

I tested it, and it seemed to work.

Let me know if this helps..

GComyn
:sleuth: