我正在尝试在其父文件夹中选择一个邮件项目。我正在使用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
答案 0 :(得分:0)
为什么你想要遍历文件夹中的所有项目?如果您知道该项的条目ID,只需使用Namespace.GetItemFromID
打开它,阅读其父条目ID(MailItem.Parent.EntryID
属性),然后将其与文件夹的条目ID进行比较有问题使用Namespace.ComparerEntryIDs
。如果匹配,只需将项目添加到选择中。