MailItem发送无效 [英] MailItem Sent Invalid Use
问题描述
在这种情况下,我想知道为什么如果我将电子邮件设置为对象,我会在 MailItem.Sent属性。
问题
通过向项目添加outlook引用:
错误无效使用属性(.Sent)的代码:
SetEmailAsObjectCode
Dim olApp As Object:设置olApp = CreateObject(Outlook.Application)
Dim EmailToSend As Object
设置EmailToSend = Nothing
设置EmailToSend = olApp.CreateItem(0)
使用EmailToSend
On Error Resume Next
调用.Sent
如果Err.Number = 0 Then'4. If Err.Number = 0
Cells(1,1).Value =ErrorOutLookTimeout:Email not sent
Else4.如果Err.Number = 0
单元格(1,1).Value =已发送电子邮件!
End If'4.如果Err.Number = 0
错误GoTo 0
结束
工作代码:
SetCreateItemObjectCode
Dim olApp As Outlook.Application:设置olApp = CreateObject(Outlook.Application)
Dim EmailToSend As Outlook.MailItem
设置EmailToSend = Nothing
设置EmailToSend = olApp.CreateItem(0)
With olApp.CreateItem(0)
On Error Resume Next
调用.Sent
如果Err.Number = 0 Then'4.如果Err.Number = 0
单元格(1,1).Value =ErrorOutLookTimeout:未发送电子邮件
Else'4.如果Err.Number = 0
单元格(1,1).Value =邮件已发送!
End If'4.如果Err.Number = 0
错误GoTo 0
结束
您可能会注意到,而不是引用创建的电子邮件对象,它立即设置
问题:
为什么代码 SetCreateItemObjectCode 有效,而 SetEmailAsObjectCode 不是?
CreateItem
返回一个对象。在哪里 Outlook.MailItem
是一个类本身,当你做 Set EmailToSend = olApp.CreateItem(0)
虽然 EmailToSend
变量调用返回的 Object
,它不再显示 .Sent
属性。因此错误。
使用这个:
With EmailToSend
打开错误Resume Next
调用.ItemProperties.Item(已发送)
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?
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")
这篇关于MailItem发送无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!