使用文件夹路径字符串在 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
Use Folder Path String to select folder in VBA in Outlook
提问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.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).
您应该能够枚举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