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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12046884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:24:00  来源:igfitidea点击:

Use Folder Path String to select folder in VBA in Outlook

vbaoutlookoutlook-vba

提问by bneigher

How can I enter a string as a folder location ex. \MySpecificEmailAddress\Inbox To select that folder in VBA using outlook? I obtained the path using:

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

... = ActiveExplorer.CurrentFolder.FolderPath

I have searched far and wide to automatically tell the script which specific folder to run a macro on without having to select the folder then run the script.

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

回答by SliverNinja - MSFT

You should be able to enumerate Session.Storesand then Store.GetRootFolder.Foldersto 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).

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

Here is a code snippet from MSDNwhich enumerates all folders under the mailbox/store root 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