none
Make Auto Folders from Macro? RRS feed

  • Question

  • Hello everybody,

    I need to create an automatic folders in outlook for organization such as

    " PJ14xxx(name of folder)

    Input(name of subfolder always the same name)

    Output(name of subfolder, always the same name)"

    Etc....

    xxx will be a number that changing

    Can we help me??

    Friday, July 18, 2014 10:47 AM

Answers

  • Hello Miguel,

    You need to use the Folders property of the Folder class. The property returns an instance of the corresponding class which provides the Add method (creates a new folder in the Folders collection). For example:

    Sub CreateFolders()  
     Dim myNamespace As Outlook.NameSpace  
     Dim myFolder As Outlook.Folder  
     Dim myNotesFolder As Outlook.Folder  
     Dim myContactFolder As Outlook.Folder   
     
     Set myNamespace = Application.GetNamespace("MAPI")  
     Set myFolder = myNamespace.GetDefaultFolder(olFolderTasks)  
    On Error GoTo ErrorHandler  
     Set myNotesFolder = _  
     myFolder.Folders.Add("My Notes Folder", olFolderNotes)  
     Set myContactFolder = _  
     myFolder.Folders.Add("My Contacts Folder", olFolderContacts)  
     Exit Sub  
    ErrorHandler:  
     MsgBox "Error creating the folder. The folder may already exist."  
     Resume Next  
    End Sub

    You may find a lot of How-to articles in the How do I...  section in the MSDN library.

    Friday, July 18, 2014 11:29 AM
  • Hi Miguel,

    You can use the Stores property of the Namespace class to get the required Store object. Then you can get the root folder and find your folders.

    Sub CreateFolders()
     Dim myNamespace As Outlook.NameSpace
     Dim stores As Outlook.stores
     Dim store As Outlook.store
     Dim rootFolder As Outlook.Folder
     
     Set myNamespace = Application.GetNamespace("MAPI")
     Set stores = myNamespace.stores
     ' the stores collection contains all stores in the profile
     Set store = stores.Item(1)
     Set rootFolder = store.GetRootFolder()
     ' find your folder in the Folders collection
     ' rootFolder.Folders
     ' then you can add subfoldes using the Folders.Add method
    End Sub

    You can use the Stores and Store objects to enumerate all folders and search folders on all stores in the current session. You can use the GetRootFolder method to enumerate the subfolders of the root folder of the Store. Unlike NameSpace.Folders which contains all folders for all stores in the current profile, Store.GetRootFolder.Folders allows you to enumerate all folders for a given Store object in the current profile.

    Sub EnumerateFoldersInStores()  
     Dim colStores As Outlook.Stores  
     Dim oStore As Outlook.Store  
     Dim oRoot As Outlook.Folder 
     
     On Error Resume Next  
     Set colStores = Application.Session.Stores  
     For Each oStore In colStores  
     Set oRoot = oStore.GetRootFolder  
     Debug.Print (oRoot.FolderPath)  
     EnumerateFolders oRoot  
     Next  
    End Sub 
     
    Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)  
     Dim folders As Outlook.folders  
     Dim Folder As Outlook.Folder  
     Dim foldercount As Integer 
     
     On Error Resume Next  
     Set folders = oFolder.folders  
     foldercount = folders.Count  
     'Check if there are any folders below oFolder  
     If foldercount Then  
     For Each Folder In folders  
     Debug.Print (Folder.FolderPath)  
     EnumerateFolders Folder  
     Next  
     End If  
    End Sub


    Monday, July 21, 2014 9:54 AM

All replies

  • Hello Miguel,

    You need to use the Folders property of the Folder class. The property returns an instance of the corresponding class which provides the Add method (creates a new folder in the Folders collection). For example:

    Sub CreateFolders()  
     Dim myNamespace As Outlook.NameSpace  
     Dim myFolder As Outlook.Folder  
     Dim myNotesFolder As Outlook.Folder  
     Dim myContactFolder As Outlook.Folder   
     
     Set myNamespace = Application.GetNamespace("MAPI")  
     Set myFolder = myNamespace.GetDefaultFolder(olFolderTasks)  
    On Error GoTo ErrorHandler  
     Set myNotesFolder = _  
     myFolder.Folders.Add("My Notes Folder", olFolderNotes)  
     Set myContactFolder = _  
     myFolder.Folders.Add("My Contacts Folder", olFolderContacts)  
     Exit Sub  
    ErrorHandler:  
     MsgBox "Error creating the folder. The folder may already exist."  
     Resume Next  
    End Sub

    You may find a lot of How-to articles in the How do I...  section in the MSDN library.

    Friday, July 18, 2014 11:29 AM
  • Great!!!!!!

    Thank you !!!!!!!

    Friday, July 18, 2014 12:03 PM
  • Hi Eugene,

    I have tried your code but I have some doubts.

    I don't know where have been created the folders. I am sorry but I am not expert.

    I have a little bit experience.

    I need to create the folders down the red circle .

    Kind regards

    Monday, July 21, 2014 7:04 AM
  • Do you need to create sub folders or just folders below the Folders folder in Oulook?
    Monday, July 21, 2014 7:10 AM
  • Hi,

    I need sub folders by year and inside the year the number of project.

    Monday, July 21, 2014 8:51 AM
  • Hi Miguel,

    You can use the Stores property of the Namespace class to get the required Store object. Then you can get the root folder and find your folders.

    Sub CreateFolders()
     Dim myNamespace As Outlook.NameSpace
     Dim stores As Outlook.stores
     Dim store As Outlook.store
     Dim rootFolder As Outlook.Folder
     
     Set myNamespace = Application.GetNamespace("MAPI")
     Set stores = myNamespace.stores
     ' the stores collection contains all stores in the profile
     Set store = stores.Item(1)
     Set rootFolder = store.GetRootFolder()
     ' find your folder in the Folders collection
     ' rootFolder.Folders
     ' then you can add subfoldes using the Folders.Add method
    End Sub

    You can use the Stores and Store objects to enumerate all folders and search folders on all stores in the current session. You can use the GetRootFolder method to enumerate the subfolders of the root folder of the Store. Unlike NameSpace.Folders which contains all folders for all stores in the current profile, Store.GetRootFolder.Folders allows you to enumerate all folders for a given Store object in the current profile.

    Sub EnumerateFoldersInStores()  
     Dim colStores As Outlook.Stores  
     Dim oStore As Outlook.Store  
     Dim oRoot As Outlook.Folder 
     
     On Error Resume Next  
     Set colStores = Application.Session.Stores  
     For Each oStore In colStores  
     Set oRoot = oStore.GetRootFolder  
     Debug.Print (oRoot.FolderPath)  
     EnumerateFolders oRoot  
     Next  
    End Sub 
     
    Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)  
     Dim folders As Outlook.folders  
     Dim Folder As Outlook.Folder  
     Dim foldercount As Integer 
     
     On Error Resume Next  
     Set folders = oFolder.folders  
     foldercount = folders.Count  
     'Check if there are any folders below oFolder  
     If foldercount Then  
     For Each Folder In folders  
     Debug.Print (Folder.FolderPath)  
     EnumerateFolders Folder  
     Next  
     End If  
    End Sub


    Monday, July 21, 2014 9:54 AM
  • Thanks a lot.

    I have learnt a lot with you.

    kind regards

    Monday, July 21, 2014 10:51 AM