我有以下代码行。打开 Outlook 时它工作正常,但我希望它在 Outlook 关闭时也能工作。我将代码保存在命令按钮单击事件中。
Private Sub btnSend_Click()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = GetObject("", Outlook.Application)
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "adbc@adbc.com"
.CC = ""
.BCC = ""
.Subject = "Test mail from Excel Sheet-OutLook Closed"
.Body = "This is body of the mail"
.Display
.Send
.ReadReceiptRequested = True
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
我用 GetObject 和 CreateObject 方法都试过了。如果我在关闭 Outlook 后执行此代码,它不会显示任何错误,但不会发送任何邮件。
以下代码行发送邮件,但它们在 Outlook 的发件箱中排队。当用户打开 Outlook 时,只有他们从发件箱中移出。
Private Sub btnSend_Click()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "adbc@adbc.com"
.CC = ""
.BCC = ""
.Subject = "Test mail from Excel Sheet-OutLook Closed"
.Body = "This is body of the mail"
.Display
.Send
.ReadReceiptRequested = True
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub