MailItem发送无效的使用


背景
这里的问题提供了进一步的解释。
在这种情况下,我想知道为什么如果我将电子邮件设置为对象我在MailItem.Sent属性中出现“属性的无效使用”错误。
问题
通过向项目添加Outlook引用:
代码错误使用属性(.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 

工作代码:
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 

您可能会注意到,而不是引用到创build的电子邮件对象,它立即设置
题:
为什么代码SetCreateItemObjectCode工作和SetEmailAsObjectCode不是?

CreateItem返回一个对象。 在Outlook.MailItem本身是一个类,当你做Set EmailToSend = olApp.CreateItem(0)尽pipeEmailToSendvariables适应返回的Object ,它不再公开.Sent属性。 因此错误。

用这个 :

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

如果您尝试发送邮件,则需要调用MailItem.Send方法。 如果您试图查明邮件是草稿还是发送邮件,请阅读MailItem.Sent属性。

注意“d”与“t”。