Outlook发送事件类 [英] Outlook Send Event Class

查看:301
本文介绍了Outlook发送事件类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


背景:

前一段时间,我发现一个类模块从Outlook中获取事件,具体来说,正如标题所示,我正在使用它抓住发送事件项目,知道它是否真的被发送。这是Class模块本身。我不记得我以前是怎么称呼的(我刚才把它保存起来供以后参考,但删除了在主分区中调用它的方式)。


Background:
A while ago, I discovered a class module to get events from outlook, specifically, as the title suggest, I'm using it to catch the send event item -to know if it was really sent or not-. This is the Class module itself. I can't remember how I used to call it (I just saved it for later references, but, deleted the way to call it in my main sub).

Option Explicit
Public WithEvents itm As Outlook.MailItem
Private Sub itm_Send(Cancel As Boolean)
   Dim blnSent As Boolean
   On Error Resume Next
   blnSent = itm.Sent
   If Err.Number = 0 Then
      Debug.Print "Email not sent"
   Else
    Debug.Print "Email sent")
    End If
End Sub

问题:

我忘记了在我发送电子邮件的子邮箱中怎么称呼它。我尝试了以下声明:

Problem:
I forgot how should I call it in my sub that's sending the email. I have tried the following declarations at top:

Dim itmevt As New CMailItemEvents
Public EmailToSend As Outlook.MailItem

然后在我的子邮件中发送电子邮件:

Then in my sub that's sending the email:

Set itmevt.itm = EmailToSend

然而,我可以'

具体问题:

1.如何调用课堂正确吗

2.如何获取发送/未有效发送的值(我想将它写入单元格以供稍后分析-sent / not sent-)我想到解析到一个公共函数,那可能会把这个值称为sub,但是我不认为这是最好的方法。

Specific Questions:
1. How do I call the class correctly?
2. How can I get the value sent/not sent efficiently (I'd like to write it in a cell for later analysis -sent/not sent-) I thought of parsing to a public function, that could get back the value to the sub that's calling it, but, I don't think that's the best approach

推荐答案

从我能够锻炼这个班有点虚伪。正确的用法是 Set itmevt.itm = OutApp.CreateItem(0)。问题在于使用发送事件来测试是否发送了该项目。请注意事件的Cancel参数。设置取消= True 阻止发送电子邮件。这告诉我们,如果电子邮件没有发送,直到这个事件完成。发送将始终返回false,并且不会导致发送事件的错误。

From what I was able to workout this class is somewhat bogus. The proper usage would be Set itmevt.itm = OutApp.CreateItem(0). The problem lies in using the Send event to test whether or not the item was sent. Notice that the Cancel parameter of the event. Setting Cancel = True prevent the email from being sent. That tells us that if the email isn't sent till after this event completes. Sent will always return false and will never cause an error from the Send event.

另一方面,如果我们在发送电子邮件后测试看到MailItem.Sent将抛出一个该项已被移动或删除。错误。

On the other hand, if we test to see MailItem.Sent after we send the email it will throw a The item has been moved or deleted. error.

知道我们可以创建一个将发送我们的电子邮件,如果发送电子邮件将返回True,否则为False。

Knowing that we can create a function that will send our emails that will return True if the email was sent and False if it wasn't.

Function SendEmail(addressTo As String, addressCC As String, Subject As String, HTMLBody As String) As Boolean
    Dim OutApp As Object
    Set OutApp = CreateObject("Outlook.Application")

    With OutApp.CreateItem(0)
        .To = addressTo
        .CC = addressCC
        'OutMail.BCC = ""
        .Subject = Subject
        .HTMLBody = HTMLBody
        .Send
        On Error Resume Next

        Call .Sent

        SendEmail = Err.Number <> 0

        If Err.Number = 0 Then
            Debug.Print "Email not sent"
        Else
            Debug.Print "Email sent"
        End If

        On Error GoTo 0

    End With



    Set OutApp = Nothing

End Function

这篇关于Outlook发送事件类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆