使用Outlook VBA选择Outlook邮件文件夹 [英] select outlook mail folder using Outlook VBA

查看:423
本文介绍了使用Outlook VBA选择Outlook邮件文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个VBA子例程,以在用户窗体的列表框中列出名称中包含"NNN"文本的任何和所有子文件夹-我有很多子文件夹,因此找到合适的子文件夹就可以了消耗.该例程运行完美.

I have created a VBA subroutine to list any and all sub-folders that have "NNN" text in the name in a list-box on a userform - I have loads of sub-folders and finding the right one is therefore time consuming. This routine works perfectly.

但是,我现在要做的是双击一个列表框项目,它在文件夹层次结构中选择"了该文件夹,以节省我手动查找它的时间(可能向下几个级别) .

However, what I now want to do is to double-click on a list-box item and it "selects" the folder in the folder hierarchy to save me the time to locate it manually (it could be several levels down).

我有一个片段可以做到这一点:

I have a snippet that does this:

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"

However, if I try and replace "F" with a folder path which is just a string, it fails. So my question is, how can I select the folder using just a string for the folder path like "paul@anymail.com\Inbox\03_Group Finance\00_Organization Chart"

谢谢

推荐答案

Sam描述的方法将执行您想要的操作.代码有一个小问题.索引从路径开始很远.如果初始引用是收件箱,则4应该为2.

The method described by Sam will do what you want. There is a small problem with the code. The index starts to far along the path. 4 should be 2 if the initial reference is to the Inbox.

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

这篇关于使用Outlook VBA选择Outlook邮件文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆