0

如何输入字符串作为文件夹位置 ex。\MySpecificEmailAddress\Inbox 使用 Outlook 在 VBA 中选择该文件夹?我使用以下方法获得了路径:

... = ActiveExplorer.CurrentFolder.FolderPath

我已经进行了广泛搜索以自动告诉脚本在哪个特定文件夹上运行宏,而无需选择文件夹然后运行脚本。

4

1 回答 1

2

您应该能够枚举Session.Stores然后Store.GetRootFolder.Folders访问给定邮箱的所有文件夹。根据您需要深入多少级,这可能需要更多的努力(即递归)。

这是来自 MSDN 的代码片段,它枚举了邮箱/存储根文件夹下的所有文件夹:

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
于 2012-08-21T12:55:53.523 回答