发送的邮件项目无效使用

2024-04-21


背景
:
The 在这里提问 https://stackoverflow.com/questions/38405423/outlook-send-event-class/38407819#38407819提供了进一步的解释。
在这种情况下,我想知道为什么如果我将电子邮件设置为对象,我会在MailItem.Sent 属性 https://msdn.microsoft.com/en-us/library/office/ff868242.aspx.
Problem

通过添加对项目的 Outlook 引用:
错误代码无效使用属性(.Sent):
将电子邮件设置为对象代码

Dim olApp As Object: Set olApp = CreateObject("Outlook.Application")
Dim EmailToSend As Object
Set EmailToSend = Nothing
    Set EmailToSend = olApp.CreateItem(0)
    With EmailToSend
    On Error Resume Next
    Call .Sent
    If Err.Number = 0 Then ' 4. If Err.Number = 0
    Cells(1,1).Value = "ErrorOutLookTimeout: Email not sent"
    Else ' 4. If Err.Number = 0
    Cells(1,1).Value = "Email Sent!"
    End If ' 4. If Err.Number = 0
    On Error GoTo 0
    End With

工作代码:
设置创建项目对象代码

Dim olApp As Outlook.Application: Set olApp = CreateObject("Outlook.Application")
    Dim EmailToSend As Outlook.MailItem
    Set EmailToSend = Nothing
        Set EmailToSend = olApp.CreateItem(0)
        With olApp.CreateItem(0)
        On Error Resume Next
        Call .Sent
        If Err.Number = 0 Then ' 4. If Err.Number = 0
        Cells(1, 1).Value = "ErrorOutLookTimeout: Email not sent"
        Else ' 4. If Err.Number = 0
        Cells(1, 1).Value = "Email Sent!"
        End If ' 4. If Err.Number = 0
        On Error GoTo 0
        End With

您可能会注意到,它不是引用创建的电子邮件对象,而是立即设置
问题:

为什么代码设置创建项目对象代码作品和将电子邮件设置为对象代码 is not?


CreateItem返回一个对象。然而Outlook.MailItem本身就是一个类,当你这样做时Set EmailToSend = olApp.CreateItem(0)虽然EmailToSend变量容纳返回的Object,它不再暴露.Sent财产。因此出现错误。

用这个 :

With EmailToSend
On Error Resume Next
Call .ItemProperties.Item("Sent")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

发送的邮件项目无效使用 的相关文章

随机推荐