- Joined
- Jan 13, 2011
- Messages
- 3
- Reaction score
- 0
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:
Code:
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