创建 VBA 宏以保存电子邮件副本

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

Creating VBA macro to save email copy

vbaoutlookoutlook-vba

提问by Fred

I use Outlook (MS Exchange) and have an individual as well as two group inboxes (I'm working logged in with the individual profile through which I also have access to the group inboxes).

我使用 Outlook (MS Exchange) 并且有一个个人以及两个组收件箱(我正在使用个人配置文件登录,我也可以通过它访问组收件箱)。

When I send an email, I chose either my individual or one of the two group email addresses in the Fromfield. When the email is sent, I want a copy saved in the inbox of myIndividualMailbox, groupAMailbox, or groupBMailboxdepending on which Fromemail address I used.

当我发送电子邮件时,我在From字段中选择了我的个人或两个群组电子邮件地址之一。发送电子邮件时,我希望将副本保存在、 或的收件箱中myIndividualMailbox,具体取决于我使用的电子邮件地址。groupAMailboxgroupBMailboxFrom

Example:If I send an email From[email protected], I want a copy of the email saved in the inbox of the groupAMailbox(and not in my individual inbox).

示例:如果我发送电子邮件From[email protected],我想要保存在groupAMailbox(而不是我的个人收件箱)收件箱中的电子邮件副本。

I have understood that this is not possible by setting up a rule in Outlook but that it could be done with a VBA macro. I don't now how to write the VBA macro and don't know if this is a just a short script or more complicated. In fact I have never written a macro in Outlook so I don't even know how to begin. Can anyone show how to do this?

我知道这无法通过在 Outlook 中设置规则来实现,但可以使用VBA 宏来完成。我现在不知道如何编写 VBA 宏,也不知道这是一个简短的脚本还是更复杂的脚本。事实上,我从未在 Outlook 中编写过宏,所以我什至不知道如何开始。任何人都可以展示如何做到这一点?

I started looking for a solution with this question: Outlook send-rule that filter on the 'From' field

我开始寻找这个问题的解决方案:Outlook send-rule that filter on the 'From' field

回答by Daniel

I made this for you as far as I can tell, it works. You should put this in the Microsoft Outlook Objects - ThisOutlookSession Module.

据我所知,我为你做了这个,它有效。您应该将它放在 Microsoft Outlook 对象 - ThisOutlookSession 模块中。

Note that the myolApp_ItemSendevent will never trigger unless you run enableEventsfirst. And you will need to make sure it is enabled every time you close an re-open Outlook. This will take some customization, but it should give you the general idea.

请注意,myolApp_ItemSend除非您enableEvents先运行,否则该事件永远不会触发。并且每次关闭重新打开的 Outlook 时,您都需要确保它已启用。这将需要一些自定义,但它应该给你一般的想法。

Option Explicit
Public WithEvents myolApp  As Outlook.Application

Sub enableEvents()
    Set myolApp = Outlook.Application
End Sub

Private Sub myolApp_ItemSend(ByVal item As Object, Cancel As Boolean)
    Dim items As MailItem
    Dim copyFolder As Outlook.Folder
    Dim sentWith As String
    'Identify sender address
    If item.Sender Is Nothing Then
        sentWith = item.SendUsingAccount.SmtpAddress
    Else
        sentWith = item.Sender.Address
    End If

    'Determin copy folder based on sendAddress
    Select Case sentWith
        Case "[email protected]"
            'get groupAMailbox's inbox
            Set copyFolder = Application.GetNamespace("MAPI").folders("groupAMailbox").folders("Inbox")
        Case "myE-mailAddress"
            'get My inbox
            Set copyFolder = Application.GetNamespace("MAPI").folders("myE-mailAddress").folders("Inbox")
    End Select

    'copy the Item
    Dim copy As Object
    Set copy = item.copy
    'move copy to folder
    copy.Move copyFolder

End Sub

EDIT:It looks like they've actually built the event functionality into the Application object for Outlook directly now, but it from testing you still have to do what I outlined above.

编辑:看起来他们现在实际上已经将事件功能直接构建到 Outlook 的 Application 对象中,但是从测试中您仍然必须执行我上面概述的操作。

回答by user221600

Outlook stores all sent items in default sent items folders. however you can apply a patch to save sent items in its own folder. http://support.microsoft.com/kb/2181579

Outlook 将所有已发送邮件存储在默认已发送邮件文件夹中。但是,您可以应用补丁将发送的项目保存在其自己的文件夹中。 http://support.microsoft.com/kb/2181579