创建VBA宏以保存电子邮件副本 [英] Creating VBA macro to save email copy
问题描述
我使用Outlook(MS Exchange),有一个个人和两个组收件箱(我正在使用个人资料登录,通过该个人资料我也可以访问组收件箱).
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).
发送电子邮件时,我在From
字段中选择了我的个人或两个组的电子邮件地址之一.发送电子邮件后,我希望将副本保存在myIndividualMailbox
,groupAMailbox
或groupBMailbox
的收件箱中,具体取决于我使用的From
电子邮件地址.
When I send an email, I chose either my individual or one of the two group email addresses in the From
field. When the email is sent, I want a copy saved in the inbox of myIndividualMailbox
, groupAMailbox
, or groupBMailbox
depending on which From
email address I used.
示例:如果我发送电子邮件From
groupA@myCompany.com ,我希望将电子邮件副本保存在groupAMailbox
( ,而不是在我的个人收件箱中).
Example: If I send an email From
groupA@myCompany.com, I want a copy of the email saved in the inbox of the groupAMailbox
(and not in my individual inbox).
我了解到,无法通过在Outlook中设置规则来做到这一点,但是可以通过 VBA宏来完成.我现在不知道如何编写VBA宏,也不知道这是一个简短的脚本还是更复杂的脚本.实际上,我从未在Outlook中编写宏,所以我什至不知道如何开始.谁能展示如何做到这一点?
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在发件人"字段中进行过滤的发送规则
I started looking for a solution with this question: Outlook send-rule that filter on the 'From' field
推荐答案
据我所知,它是为您准备的,它可以正常工作.您应该将其放在Microsoft Outlook对象-ThisOutlookSession模块中.
I made this for you as far as I can tell, it works. You should put this in the Microsoft Outlook Objects - ThisOutlookSession Module.
请注意,除非首先运行enableEvents
,否则myolApp_ItemSend
事件将永远不会触发.而且,您需要确保每次关闭重新打开的Outlook时都启用该功能.这将需要一些自定义,但应该可以为您提供总体思路.
Note that the myolApp_ItemSend
event will never trigger unless you run enableEvents
first. 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.
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 "groupA@myCompany.com"
'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
看来,他们现在实际上已经直接将事件功能内置到Outlook的Application对象中,但是通过测试,您仍然必须执行我上面概述的操作.
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.
这篇关于创建VBA宏以保存电子邮件副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!