【问题标题】:Select item after selecting parent folder选择父文件夹后选择项目
【发布时间】:2020-06-24 21:14:46
【问题描述】:

我正在尝试在其父文件夹中选择一个邮件项目。我正在使用 Outlook 2016。

宏执行以下操作:

  • 在资源管理器中获取选定的邮件项
  • 选择它的父文件夹
  • 选择文件夹中的邮件项目

代码在调试模式下工作,但在运行时不工作。我猜在运行时资源管理器需要一些时间才能打开。

我尝试使用 Sleep() 方法等待资源管理器。

Public Sub SelectSelectedItemInParentFolder()    

    Dim outlook As Object
    Dim x As Long
    Dim strCats As String
    Dim arrSelection As Object
    Set outlook = CreateObject("Outlook.Application")
    Set arrSelection = GetCurrentSelection
    Dim folder As outlook.MAPIFolder
    Dim mail As outlook.MailItem

    If arrSelection.Count > 1 Then
        MsgBox ("Nothing selected")
        Exit Sub
    End If

    If arrSelection.item(1).Class = OlObjectClass.olMail Then

        Set mail = arrSelection.item(1)

    End If

    Set folder = arrSelection.item(1).Parent

    If Not (folder Is Nothing) Then

        ' this works fine at runtime, the folder is selected
        Set Application.ActiveExplorer.CurrentFolder = folder

        ' below code works fine only in debug mode, when steping into the code.
        ' At runtime, it seems that the explorer is not yet loaded when the code runs       
        If Not (mail Is Nothing) Then

            For x = 1 To folder.Items.Count

                If folder.Items(x).Class = OlObjectClass.olMail Then

                    If folder.Items(x).EntryID = mail.EntryID Then

                        Application.ActiveExplorer.ClearSelection
                        Application.ActiveExplorer.AddToSelection (folder.Items(x))
                        x = folder.Items.Count

                    End If

                End If

            Next x

        End If

    End If

End Sub

【问题讨论】:

  • 你想对选中的项目做什么?
  • 有时无法像调试器一样弄清楚如何减慢进程。这个stackoverflow.com/a/52023490/1571407 描述了三个可能的选项 DoEvents、DoEvents 和 Msgbox 并循环直到错误条件清除。如果您找到答案,请记得在此处发布。

标签: vba outlook


【解决方案1】:

您为什么要遍历文件夹中的所有项?如果您知道项目的条目 ID,只需使用 Namespace.GetItemFromID 打开它,读取其父条目 ID(MailItem.Parent.EntryID 属性),然后使用 Namespace.ComparerEntryIDs 将其与相关文件夹的条目 ID 进行比较。如果它们匹配,只需将该项目添加到选择中。

【讨论】:

  • 其实是因为我的商品是搜索的结果。我想要做的是使用搜索框搜索项目,如果找到,则使用鼠标选择项目,然后调用宏打开项目的父文件夹,最后选择父文件夹中的项目。这是我做不到的最后一个动作。
  • 但这不是您的代码正在做的事情。你在找什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-14
  • 2012-08-01
  • 1970-01-01
  • 2014-07-31
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多