Suhas14
Suhas14

Reputation: 1

Create folder in root folder of a specific Outlook account using Excel VBA

I want to create a folder in Outlook that contains a set of accounts.

I need to create the folder in a particular email account using Excel VBA

Upvotes: 0

Views: 162

Answers (1)

Eugene Astafiev
Eugene Astafiev

Reputation: 49395

It seems you need to automate Outlook from Excel VBA and create a folder in a profile. The Automating Outlook from a Visual Basic Application article describes all the required steps for that.

If you have got several accounts configured in the Outlook profile you can use the Stores object which represents a set of Store objects representing all the stores available in the current profile. For example:

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

A profile defines one or more email accounts, and each email account is associated with a server of a specific type. For an Exchange server, a store can be on the server, in an Exchange Public folder, or in a local Personal Folders File (.pst) or Offline Folder File (.ost). For a POP3, IMAP, or HTTP email server, a store is a .pst file.

You can use the Stores and Store objects to enumerate all folders and search folders on all stores in the current session. Since getting the root folder or search folders in a store requires the store to be open and opening a store imposes an overhead on performance, you can check the Store.IsOpen property before you decide to pursue the operation.

Once the Store object is found, you can use the GetDefaultFolder or GetRootFolder of the Store class to find the folder where a new folder should be created.

Finally, you may find the Storing Outlook Items page helpful.

Upvotes: 0

Related Questions