VBA Outlook Mail .display,记录手动发送时/如果

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

VBA Outlook Mail .display, recording when/if sent manually

vbaoutlookms-officeoffice-2007outlook-2007

提问by ExcelCyclist

My code displays a message with basic subject, body, attachment. Next the user manually updates and customizes the message and should send it. I want to record when (if) the email is sent. Is this possible or any tips?

我的代码显示带有基本主题、正文、附件的消息。接下来,用户手动更新和自定义消息并应该发送它。我想记录发送电子邮件的时间(如果)。这是可能的或任何提示吗?

My environment is Office 2007 with an excel based macro going to Outlook.

我的环境是 Office 2007,有一个基于 excel 的宏可以转到 Outlook。

[Excerpt]

[摘抄]

Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon

Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
    .To = Email                 '.CC = 
    .Subject = Subj
    .BodyFormat = olFormatHTML
    .Body = Msg                 '.HTMLBody = Msg
    If Not FileAttach = vbNullString Then .Attachments.Add (FileAttach) 
    .Display
End With

回答by Jon Fournier

This is entirely possible, using the _Send event in the Outlook.MailItem class.

这是完全可能的,使用 Outlook.MailItem 类中的 _Send 事件。

The way I use it, I create a class called EMail Watcher, so when I create the email and do the .Display, I then create a new EMailWatcher object and tell it to watch that email for send, then report back when it happens.

我使用它的方式是创建一个名为 EMail Watcher 的类,所以当我创建电子邮件并执行 .Display 时,我然后创建一个新的 EMailWatcher 对象并告诉它监视该电子邮件以进行发送,然后在发生时进行报告。

Here's the class as I use it. Basically, I also optionally can set the BoolRange so that if the user sends the email, that Excel range gets updated with True. I can also have the class update an Excel range with the time the email is sent.

这是我使用的课程。基本上,我还可以选择设置 BoolRange,这样如果用户发送电子邮件,该 Excel 范围就会更新为 True。我还可以让班级根据发送电子邮件的时间更新 Excel 范围。

Public BoolRange As Range
Public DateRange As Range
Public WithEvents TheMail As Outlook.MailItem


Private Sub TheMail_Send(Cancel As Boolean)
    If Not BoolRange Is Nothing Then
        BoolRange.Value = True
    End If
    If Not DateRange Is Nothing Then
        DateRange.Value = Now()
    End If
End Sub

And here's how I use it:

这是我如何使用它:

With oMail
    .To = addr
    .Subject = "CCAT eVSM Utilities License Code"
    .Body = "Message body"
    .Display
End With
Set CurrWatcher = New EmailWatcher
Set CurrWatcher.BoolRange = Range("G12")
Set CurrWatcher.TheMail = oMail

Hopefully that helps...

希望这有助于...