且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用文件夹路径字符串在Outlook中的VBA中选择文件夹

更新时间:2022-11-04 21:28:36

您应该能够枚举 Session.Stores ,然后 访问给定邮箱的所有文件夹.视您需要走几级而定,这可能需要更多的精力(即递归).

You should be able to enumerate Session.Stores and then Store.GetRootFolder.Folders to access all folders for a given mailbox. Depending on how many levels deep you need to go, this may take a bit more effort (i.e. recursion).

这是一个来自MSDN的代码段,其中列举了所有邮箱/存储根文件夹下的文件夹:

Here is a code snippet from MSDN which enumerates all folders under the mailbox/store root folders:

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