【发布时间】: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 并循环直到错误条件清除。如果您找到答案,请记得在此处发布。