背景:
question here提供了進一步的解釋。
在這種情況下,我想知道爲什麼如果我將電子郵件設置爲對象,則在MailItem.Sent Property中出現「無效使用財產」的錯誤。
問題
通過添加展望參考項目:
代碼錯誤無效使用性質(.Sent)的:
SetEmailAsObjectCodeMailItem Sent Invalid Use
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是不?
我明白了,我認爲它與OOP邏輯的缺失部分有關,我還沒有完全理解,謝謝! – Sgdva
這就是COM的精彩世界的工作原理。 :) – cyboashu