Reputation: 2800
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?
Upvotes: 2
Views: 508
Reputation: 10443
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")
Upvotes: 4
Reputation: 66316
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".
Upvotes: 1