MailItem Sent Invalid Use

那年仲夏 提交于 2019-12-24 00:37:08

问题



Background
:
The question here provides a further explanation.
In this case, I want to know why if I set the email as object I get an error of "invalid use of property" in the MailItem.Sent Property.
Problem

By adding outlook reference to the project:
Code with error invalid use of property(.Sent):
SetEmailAsObjectCode

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

Working code:
SetCreateItemObjectCode

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

As you may notice, instead of referencing to the email object created it's set right away
Question:

Why does the code SetCreateItemObjectCode works and the SetEmailAsObjectCode is not?


回答1:


CreateItem return an Object. Where as Outlook.MailItem is a class itself and when you do Set EmailToSend = olApp.CreateItem(0) although the EmailToSend variable accommodates the returned Object , it no more expose the .Sent property. hence the error.

use this :

With EmailToSend
On Error Resume Next
Call .ItemProperties.Item("Sent")



回答2:


If you are trying to send a message, you need to call the MailItem.Send method. If you are trying to find out if a message is a draft or a sent one, you read the MailItem.Sent property.

Note "d" vs "t".



来源:https://stackoverflow.com/questions/38442315/mailitem-sent-invalid-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!