vba 如何移动对话中的所有消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6219460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:22:33  来源:igfitidea点击:

How to move all messages in a conversation?

vbaoutlookoutlook-vba

提问by Anthony Mastrean

I need to know how to move all of the messages in a conversation at once.

我需要知道如何一次移动对话中的所有消息。

My macro currently reads

我的宏当前读取

Sub Archive()
    Set ArchiveFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders("Archive")
    For Each Msg In ActiveExplorer.Selection
        Msg.UnRead = False
        Msg.Move ArchiveFolder
    Next Msg
End Sub

But that only moves the latest message... and only when the conversation is fully collapsed! I can't Archive when the conversation is expanded.

但这只会移动最新的消息……而且只有在对话完全崩溃时!展开对话后,我无法存档。

回答by Anthony Mastrean

Paul-Janput me on the right path, so I gave him the answer. Here's my really poor VBA version (I'm missing some type casting, checking). But it does work on collapsed and expanded conversations of mail.

Paul-Jan让我走上了正确的道路,所以我给了他答案。这是我非常糟糕的 VBA 版本(我缺少一些类型转换,检查)。但它确实适用于折叠和展开的邮件对话。

Sub ArchiveConversation()
    Set ArchiveFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders("Archive")
    Set Conversations = ActiveExplorer.Selection.GetSelection(Outlook.OlSelectionContents.olConversationHeaders)
    For Each Header In Conversations
        Set Items = Header.GetItems()
        For i = 1 To Items.Count
            Items(i).UnRead = False
            Items(i).Move ArchiveFolder
        Next i
    Next Header
End Sub

回答by James Frank

Anthony's answer almostworks for me. But it doesn't work on both messages and conversations. Here's my modification:

安东尼的回答几乎对我有用。但它不适用于消息和对话。这是我的修改:

Sub Archive()
    Set ArchiveFolder = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders("Archive")

    Dim IsMessage As Integer
    IsMessage = 0

    For Each Msg In ActiveExplorer.Selection
        Msg.Move ArchiveFolder
        IsMessage = 1
    Next Msg

    If IsMessage = 0 Then
        Set Conversations = ActiveExplorer.Selection.GetSelection(Outlook.OlSelectionContents.olConversationHeaders)
        For Each Header In Conversations
            Set Items = Header.GetItems()
            For i = 1 To Items.Count
                Items(i).UnRead = False
                Items(i).Move ArchiveFolder
            Next i
        Next Header
    End If

End Sub

回答by Paul-Jan

If you want to handle conversations, you'll have to do so explicitly. You can go from MailItem to its Conversation using MailItem.GetConversation, but you'd be better off working with conversations directly.

如果你想处理对话,你必须明确地这样做。您可以使用MailItem.GetConversation从 MailItem 转到其 Conversation ,但最好直接处理对话。

What you do is:

你要做的是:

  1. Get all conversation headers from the current selection
  2. For each conversation, get the individual items
  3. Do your archiving thing with them.
  1. 从当前选择中获取所有对话标题
  2. 对于每个对话,获取单个项目
  3. 和他们一起做你的归档工作。

The following C# code illustrates this, and should be trivial to port to VBA.

以下 C# 代码说明了这一点,移植到 VBA 应该很简单。

Outlook.Selection selection = Application.ActiveExplorer().Selection;
Outlook.Selection convHeaders = selection.GetSelection( Outlook.OlSelectionContents.olConversationHeaders) as Outlook.Selection;
foreach (Outlook.ConversationHeader convHeader in convHeaders)
{
  Outlook.SimpleItems items = convHeader.GetItems();
  for (int i = 1; i <= items.Count; i++)
  {
    if (items[i] is Outlook.MailItem)
    {
      Outlook.MailItem mail =  items[i] as Outlook.MailItem;
      mail.UnRead = false;
      mail.Move( archiveFolder );
    }
    // else... not sure how if you want to handle different types of items as well  }
}