【发布时间】:2021-08-23 01:05:40
【问题描述】:
我的情况:
我正在尝试在 Outlook 未打开时发送电子邮件。如果 Outlook 正在运行,此代码将起作用。如果 Outlook 已关闭,代码将创建一个不可见的进程,您可以在任务管理器中看到该进程正在运行。
代码在.Send 期间遇到错误。它返回运行时错误 287。
根据我的经验,某些 VBA 方法仅在对象可见或活动时才有效。
我的解决方法是在调用.Send 之前使用.Display(False)。
调用.Send 后,它会立即终止 Outlook 进程。 (为什么请指出正确的方向。)然后电子邮件卡在发件箱中。
我必须通过调用另一个CreateObject("Outlook.Application") 并循环通过发件箱While Outbox.Items.Count > 0 来应用另一个解决方法
或
循环通过SyncObjects 并手动调用.Start 以在发件箱上运行发送/接收。
我的问题:
有没有办法直接使用.Send 方法而不是使用变通方法,同时也不必打开 Outlook。
我的代码:
Sub emailer()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.to = "f@r.com"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Send 'or use .Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
【问题讨论】:
-
尝试将“While Outbox.Items.Count > 0”循环放入代码中,紧跟在“End With”行之后