PDA

View Full Version : Move Email in an IMAP Account



bluepeople12
01-13-2011, 02:45 AM
I have to write a VBA Script where I can move Email from an IMAP Account to an Folder within this IMAP Account, but I don't know where to start. Here is some code I found in the internet, but this code always shows that the folder doesn't exist:

Sub MoveMessagesToFolder(strEmail As String, strOrdner As String)
On Error Resume Next
'der MAPI-Folder legt den Ordner fest (z.B. Zielordner)
Dim objFolder As Outlook.MAPIFolder
'dieser MAPI-Folder legt den Ordner Posteingang fest
Dim objInbox As Outlook.MAPIFolder
'mit den Namespaces wird der aktuelle Arbeitsbereich
'von Outlook festgelegt und stellt Methoden zur
'An- und Abmeldung in Outlook bereit.
Dim objNS As Outlook.NameSpace
'die Items
Dim objItem As Outlook.MailItem
Set objNS = Application.GetNamespace("MAPI")
'Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objOtherInbox = Session.Folders("[Email-Account-Name]\Posteingang")
Set objFolder = objOtherInbox.Folders(strOrdner)

'Assume this is a mail folder
If objFolder Is Nothing Then
MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
End If

For Each objItem In Application.ActiveExplorer.Session
If objFolder.DefaultItemType = olMailItem Then
If (objItem.Class = olMail) And (objItem.SenderEmailAddress = strEmail) Then
objItem.Move objFolder
End If
End If
Next

Set objItem = Nothing
Set objFolder = Nothing
Set objInbox = Nothing
Set objNS = Nothing
End Sub

gcomyn
01-21-2011, 01:42 PM
I'm guessing that the "Posteingang" folder is on the same level as the Inbox... here is how I set my folders.


Set objFolder = objNS.GetDefaultFolder(olFolderInbox).Parent.Folders("Posteingang")


That would set the folder if it was in the same mailbox as your default inbox... I'm not sure how to set it to another account within the same session...

GComyn
:sleuth: