【发布时间】:2016-07-18 17:03:02
【问题描述】:
背景:
question here 提供了进一步的解释。
在这种情况下,我想知道为什么如果我将电子邮件设置为对象,我会在MailItem.Sent Property 中收到“无效使用属性”的错误。
问题
通过向项目添加 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
您可能会注意到,它没有引用创建的电子邮件对象,而是立即设置
问题:
为什么代码 SetCreateItemObjectCode 有效而 SetEmailAsObjectCode 无效?
【问题讨论】: