使用Outlook VBA选择Outlook邮件文件夹

时间:2018-05-29 12:09:04

标签: vba outlook directory

我创建了一个VBA子例程,用于列出在用户窗体上的列表框中名称中包含“NNN”文本的所有子文件夹 - 我有很多子文件夹,因此找到正确的子文件夹是时间耗时。这个例程很有效。

但是,我现在要做的是双击列表框项目并“选择”文件夹层次结构中的文件夹以节省我手动定位的时间(可能是几个级别) 。

我有一个代码片段:

Public Sub GetItemsFolderPath()
  Dim obj As Object
  Dim F As Outlook.MAPIFolder
  Dim Msg$
  Set obj = Application.ActiveWindow
  If TypeOf obj Is Outlook.Inspector Then
    Set obj = obj.CurrentItem
  Else
   Set obj = obj.Selection(1)
  End If
  Set F = obj.Parent
  Msg = "The path is: " & F.FolderPath & vbCrLf
  Msg = Msg & "Switch to the folder?"
  If MsgBox(Msg, vbYesNo) = vbYes Then
   Set Application.ActiveExplorer.CurrentFolder = F
  End If
End Sub

但是,如果我尝试将“F”替换为仅仅是字符串的文件夹路径,则会失败。 所以我的问题是,我如何只使用文件夹路径的字符串选择文件夹,如“paul@anymail.com \ Inbox \ 03_Group Finance \ 00_Organization Chart”

由于

2 个答案:

答案 0 :(得分:0)

我尝试了一个简单的小事来从路径中返回一个文件夹:

Function FolderFromPath(FolderPath As String) As Folder
    Dim F As Folder
    Dim arrFolders() As String
    Dim i As Integer
    Set myNamespace = Application.GetNamespace("MAPI")

    Set F = myNamespace.GetDefaultFolder(olFolderInbox)
    arrFolders = Split(FolderPath, "\")
    For i = 4 To UBound(arrFolders)
        Set F = F.Folders(arrFolders(i))
    Next
    Set FolderFromPath = F
 End Function

它从您的收件箱开始(可能不是您需要的),然后拆分路径并进入路径中的每个文件夹。

评论后更新 我忘了展示如何使用它。你可以这样做:

Path = "\\first.last@company.com\Inbox\Folder1\Folder2"
Set Application.ActiveExplorer.CurrentFolder = FolderFromPath(Path)

答案 1 :(得分:0)

Sam描述的方法会做你想要的。代码有一个小问题。该指数沿着这条道路开始走远。如果初始引用是收件箱,则4应为2。

Function FolderFromPath(FolderPath As String) As Folder

    Dim F As Folder
    Dim arrFolders() As String
    Dim i As Long

    arrFolders = Split(FolderPath, "\")

    ' Initial reference is to the mailbox - array element 0
    Set F = Session.Folders(arrFolders(0))

    ' The next folder is array element 1
    For i = LBound(arrFolders) + 1 To UBound(arrFolders)
        Set F = F.Folders(arrFolders(i))
    Next

    Set FolderFromPath = F

 End Function


Public Sub GetItemsFolderPath_Test()

    Dim FPath As String

    FPath = "paul@anymail.com\Inbox\03_Group Finance\00_Organization Chart"

    Set ActiveExplorer.CurrentFolder = FolderFromPath(FPath)

End Sub