【发布时间】:2019-09-04 17:34:38
【问题描述】:
我有以下几行代码。打开 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
【问题讨论】:
-
我没有看到 CreateObject 代码?这应该工作......
-
我使用了 CreateObject("",Outlook.Application) 而不是 GetObject("",Outlook.Application) 并且剩下的行是相同的。
-
如果我这样写,那么邮件在发件箱中排队。每当用户打开 Outlook 时,只有邮件会发送。``
-
Sub btnSend_Click() 将 OutApp 调暗为对象 将 OutMail 调暗为对象 Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) On Error Resume Next With OutMail .To = " abcd@abcd.com" .CC = "" .BCC = "" .Subject = "来自 Excel Sheet-OutLook 的测试邮件已关闭" .Body = "这是邮件正文" .Display .Send .ReadReceiptRequested = True End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing End Sub `
-
你能用评论中提到的上述代码更新你的问题吗?