我正在尝试使用宏来归档电子邮件,但是如果用户选择一个对话标题,则会遇到问题。这些标头的typeOf
是什么?
我正在尝试在选择电子邮件时获取父文件夹,以确定是否应移动(或已经移动)电子邮件。对于.MailItem
,我可以使用以下代码获取它:
Set selection = ActiveExplorer.selection
Set CurrentFolder = selection(1).Parent
但是,当仅选择对话标头时,它将返回错误“运行时错误'440':数组索引超出范围。”
当尝试使用如下if语句时:
If TypeOf selection Is Outlook.MailItem Then
Set CurrentFolder = selection(1).Parent
ElseIf TypeOf selection Is Outlook.ConversationHeader Then
'Set CurrentFolder
Else
'Return error
End If
.ConversationHeader
不起作用,因为ElseIf
语句返回False
。
我应该使用什么typeof
?然后应该使用什么代码来查找父文件夹?
答案 0 :(得分:0)
选择属性返回Selection对象,而不是单个项目。您将需要遍历选择项(使用for each
从1到for
的“ Selection.Count
”或“ Selection.Item(index)
”循环)才能到达所选项目。< / p>
您可以使用OutlookSpy查看实时Outlook对象及其属性,方法和事件。
答案 1 :(得分:0)
Selection.Item(index) will throw an exception if your selection is the ConversationHeader indicated in the supplied image.
I was unable to locate an actual type of object for that heading, but there are some ways around it. Once identified that it isn't a MailItem you can check ActiveExplorer.Selection.GetSelection(Outlook.OlSelectionContents.olConversationHeaders)
https://docs.microsoft.com/en-us/office/vba/api/outlook.selection.getselection
Most notably, they indicate
Calling GetSelection with olConversationHeaders as the argument returns a Selection object that has the Location property equal to OlSelectionLocation.olViewList.
If the current view is not a conversation view, or, if Selection.Location is not equal to OlSelectionLocation.olViewList , calling GetSelection with olConversationHeaders as the argument returns a Selection object with Selection.Count equal to 0.
Running a quick test of this
Dim oSelection = Selection
Set oSelection = ActiveExplorer.Selection.GetSelection(olConversationHeaders)
Print oSelect.Count
--returns 1 when i have that odd header selected.
--returns 0 when you have a mailItem within the header selected
Furthermore, take a look at How to move all messages in a conversation?
We can easily adjust the answers given to something along the lines of :
Set conversations = ActiveExplorer.Selection.GetSelection(Outlook.OlSelectionContents.olConversationHeaders)
Dim sItems As SimpleItems
For Each Header In conversations
Set Items = Header.GetItems()
For i = 1 To Items.Count
If TypeOf Items(i) Is Outlook.MailItem Then
Debug.Print (Items(i).Parent)
End If
Next i
Next Header
End Sub
Notably this only returned headers for emails in the selected group that were IN the folder the selection was made. I believe you'd have to further up the ante-by going into the getConversations and Conversations objects to travel down/upstream.