【问题标题】:How to send mail when outlook is closedOutlook关闭时如何发送邮件
【发布时间】: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 `
  • 你能用评论中提到的上述代码更新你的问题吗?

标签: vba excel


【解决方案1】:

你可以在发送邮件之前使用shell命令实际打开outlook。正是

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Public Sub OpenOutlook()
Dim ret As Long
 On Error GoTo aa
 ret = ShellExecute(Application.hwnd, vbNullString, "Outlook", vbNullString, "C:\", SW_SHOWNORMAL)
 If ret < 3 Then

 MsgBox "Outlook is not found.", vbCritical, "SN's Customised Solutions"
 End If
aa:
End Sub

将其保存在一个单独的模块中,并从您发送邮件的代码中调用该模块。我正在尝试处理的部分是如何隐藏它以便仍然使用 excel 激活

【讨论】:

    【解决方案2】:

    对于 Outlook 2013,这是 Outlook 设置的问题,而不是 VBA 代码的问题。

    • 展望未来

    • 转到文件 -> 选项 -> 高级

    • 滚动到“发送和接收”标题并点击“发送/接收...”按钮

    • 在“组“所有帐户”的设置”下,确保“执行 退出时自动发送/接收'被选中

    这可确保在 Outlook 关闭时发送 OUTLOOK“发件箱”中的所有项目。这为我解决了这个问题。其他版本的 Outlook 可能类似。

    【讨论】:

      猜你喜欢
      • 2017-01-07
      • 1970-01-01
      • 2011-12-09
      • 2021-05-11
      • 2018-05-24
      • 2017-08-29
      • 1970-01-01
      • 2017-06-08
      相关资源
      最近更新 更多