Originally Posted by
skatonni
Try .Folders rather than .Items
Set rootFolder = rootFolder.Folders(arrFolders(I))
No, it doesn't work.
Now I have included a function to test if the folder is already created. If not, it has to be created:
Public Function GetFolder(strFolderPath As String) As MAPIFolder
' strFolderPath needs to be something like
' "Public Folders\All Public Folders\Company\Sales" or
' "Personal Folders\Inbox\My Folder"
Dim objApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim objFolder As Outlook.MAPIFolder
Dim arr() As String
Dim n As Long
On Error Resume Next
strFolderPath = Replace(strFolderPath, "/", "\")
arr() = Split(strFolderPath, "\")
Set objApp = Application
Set objNS = objApp.GetNamespace("MAPI")
Set objFolder = objNS.Folders.Item(arr(0))
If Not objFolder Is Nothing Then
For n = 1 To UBound(arr)
Set colFolders = objFolder.Folders
Set objFolder = Nothing
Set objFolder = colFolders.Item(arr(n))
If objFolder Is Nothing Then
Exit For
End If
Next
End If
Set GetFolder = objFolder
Set colFolders = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Function
Sub AddFolders()
Dim myNameSpace As Outlook.NameSpace
Dim rootFolder As Outlook.Folder
Dim myNewFolder As Outlook.Folder
Dim strComputer As String
Dim arrFolders() As String
Dim I As Long
Set myNameSpace = Application.GetNamespace("MAPI")
Set fso = CreateObject("Scripting.FileSystemObject")
Set objInputFile = fso.OpenTextFile("C:\temp\test.txt", 1, True)
Do Until objInputFile.AtEndOfLine
strComputer = objInputFile.ReadLine
arrFolders() = Split(strComputer, "\")
Set rootFolder = myNameSpace.Folders("Root") '-> The account witnin Outlook which is not the main one
Set objFolder = GetFolder("Root\" + strComputer) '-> Check if the folder exists
' If the folder does not exist then we create it
If objFolder Is Nothing Then
For I = 0 To (UBound(arrFolders) - 1) '-> Buid the path until the second to last folder
Set rootFolder = rootFolder.Folders(arrFolders(I)) '-----------------------------------------> That line does not work
Next
Set myNewFolder = rootFolder.Foder.Add(arrFolders(I+1)) '-> Folder creation with the last element of the array
Erase arrFolders '-> array cleaning
End If
Loop
objInputFile.Close
Set rootFolder = Nothing
Set myNewFolder = Nothing
Set myNameSpace = Nothing
End Sub
Another idea?