Am Sun, 14 Aug 2005 05:25:18 -0700 schrieb Lp12:
Wow
You should learn something about the variable types. E.g. the Set
statement is for objects only, and you cannot set an object to a string.
Please compare my sample with yours:
Sub GetRootFolder()
Dim mpfRoot As Outlook.MAPIFolder
Dim mpf As Outlook.MAPIFolder
Dim idx As Integer
Dim Fileidx As Integer
Dim Subjectname As String
Dim SubjectnameFolder As MAPIFolder
Dim Subjectnamepoz As Long
Dim obj As Object
Dim MyItem As Outlook.MailItem
Dim CopyItem As Outlook.MailItem
Set mpf = Application.Session.GetDefaultFolder(olFolderInbox)
Set mpfRoot = mpf.Parent
'External loop for each mail item in Inbox
For Fileidx = 1 To mpf.Items.Count
Set obj = mpf.Items(Fileidx)
' Check the item´s type
If TypeOf obj Is Outlook.MailItem Then
'In this sample handle MailItems only.
Set MyItem = obj
Set CopyItem = MyItem.Copy
Subjectname = CopyItem.Subject
Subjectnamepoz = InStr(1, Subjectname, " ")
Subjectname = Left(Subjectname, Subjectnamepoz - 1)
' Check for the folder in a separate function because if it
doesn´t exist _
then my used method raises an error.
Set SubjectnameFolder = CheckForFolder(mpfRoot.Folders,
Subjectname)
If SubjectnameFolder Is Nothing Then
' Folder doesn´t exist. Create first then move item into
it.
Set SubjectnameFolder = mpfRoot.Folders.Add(Subjectname)
CopyItem.Move SubjectnameFolder
End If
' 'Internal loop to find/create the destination folder.
' For idx = 1 To mpfRoot.Folders.Count
' MsgBox mpfRoot.Folders.Item(idx).Name
' If mpfRoot.Folders.Item(idx).Name = Subjectname Then
' CopyItem.Move SubjectnameFolder
' mpfRoot.Folders.Add Subjectname
' End If
' Next
End If
Next
End Sub
Private Function CheckForFolder(colFolders As Outlook.Folders, _
sName As String) _
As Outlook.MAPIFolder
On Error Resume Next
Set CheckForFolder = colFolders(sName)
End Function