简体   繁体   中英

How to find Achieve folders in Outlook without knowing the file path using C#

I am building an internal VSTO Outlook add-in and I need to scan all e-mails for certain numbers. I have been successful in scanning all mails on the Exchange Server, but I am having problems with the local Achive folders.

I have tried to look for many hours for a solution, but with no luck. All examples rely on the folder being named "Archive" or another known name that can then be hardcoded, which results in solutions like this:

private static _NameSpace ns = myApp.GetNamespace("MAPI");
Outlook.Folder archiveFolder = ns.Folders["Archive"] as Outlook.Folder;

How the structure might look like

This above link shows the structure i will likely encounter among my coworkers. (With MANY more folders)

I am looking for a solution that will find ALL these folders and their content OR the root folder path. I have a method that can find all the subfolders, which i can post if needed.

I hope someone can help me:)

It seems you just need to iterate over all stores in the profile by using the Stores property of the Namespace class which returns a Stores collection object that represents all the Store objects in the current profile. You can use the Stores and Store objects to enumerate all folders and search folders on all stores in the current session.

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

Read more about that in the Working with Outlook Accounts, Stores, Folders and Items article.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM