vba 如何创建脚本将收件箱中当前活动的电子邮件移动到 Outlook 2007 中的另一个文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/773206/
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 10:23:39  来源:igfitidea点击:

How can I create a script to move the currently active email in the Inbox to another folder in Outlook 2007

vbaoutlookoutlook-2007outlook-vba

提问by Keng

I sometimes get emails that I want to keep but to move them into the appropriate folder can be a pain. How can I execute a script that will move (like using C-S-v) the email I'm looking at into a certain folder called "buffer", for instance?

我有时会收到我想保留的电子邮件,但将它们移动到适当的文件夹中可能会很痛苦。例如,如何执行将我正在查看的电子邮件移动(如使用 CSV)到某个名为“缓冲区”的文件夹中的脚本?

I'm using Outlook 2007.

我正在使用 Outlook 2007。

thanks.

谢谢。



EDIT: there isn't any criteria that can be created to automate this process like through a rule. it is merely a judgment call I make as i'm staring at it.

编辑:没有任何标准可以像通过规则一样创建来自动化这个过程。这只是我在注视它时做出的判断。

采纳答案by Keng

Here's the code I'm using.

这是我正在使用的代码。

Sub MoveSelectedMessagesToFolder()
'Originally written by Chewy Chong
'Taken from http://verychewy.com/archive/2006/04/12/outlook-macro-to-move-an-email-to-folder.aspx
'Thanks Chewy!
'Ken
On Error Resume Next
    Dim objFolder As Outlook.MAPIFolder, objInbox As Outlook.MAPIFolder
    Dim objNS As Outlook.NameSpace, objItem As Outlook.MailItem

    Set objNS = Application.GetNamespace("MAPI")
    Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
    'For the "Item" portion, I used the name of the folder exactly as it appear in the ToolTip when I hover over it.
    Set objFolder = objNS.Folders.Item("Personal Folders").Folders.Item("Buffer")

'Assume this is a mail folder

    If objFolder Is Nothing Then
        MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
    End If

    If Application.ActiveExplorer.Selection.Count = 0 Then
        'Require that this procedure be called only when a message is selected
        Exit Sub
    End If
    For Each objItem In Application.ActiveExplorer.Selection
        If objFolder.DefaultItemType = olMailItem Then
            If objItem.Class = olMail Then
                objItem.Move objFolder
            End If
        End If
    Next

回答by JimmyPena

This code may work better.

此代码可能效果更好。

In your code, objFolder may be equal to Nothing, yet you continue the procedure. Also, the For Each loop assumes that each item is a mail item.

在您的代码中,objFolder 可能等于 Nothing,但您继续执行该过程。此外,For Each 循环假定每个项目都是邮件项目。

Sub MoveSelectedMessagesToFolder()
  Dim objNS As Outlook.NameSpace
  Dim objFolder As Outlook.MAPIFolder
  Dim obj As Object
  Dim msg As Outlook.mailItem

  Set objNS = Application.GetNamespace("MAPI")
  On Error Resume Next
  Set objFolder = objNS.Folders.item("Personal Folders").Folders.item("Buffer")
  On Error GoTo 0

  If objFolder Is Nothing Then
    MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation, "INVALID FOLDER"
    Exit Sub
  End If

  For Each obj In ActiveExplorer.Selection
    If TypeName(obj) = "MailItem" Then
      Set msg = obj
      msg.Move objFolder
    End If
  Next obj

End Sub

回答by Tom Ritter

Tools -> Rules & Alerts

工具 -> 规则和警报

Then Create a new rule telling all mail that fit whatever criteria to be deleted/marked as read/moved to a folder/any combination of those.

然后创建一个新规则,告诉所有符合任何标准的邮件被删除/标记为已读/移动到文件夹/这些的任何组合。

Edit:If you don't want a rule/can't make a rule that fits, you can create a Macro (Tools -> Macro) to move it to a folder, then bind it to a shortcut.

编辑:如果您不想要规则/无法制定适合的规则,您可以创建一个宏(工具 -> 宏)将其移动到文件夹,然后将其绑定到快捷方式。