1

我有以下代码行。打开 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
4

2 回答 2

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

于 2013-06-13T07:19:41.630 回答
1

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

  • 打开展望

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

  • 滚动到“发送和接收”标题,然后单击“发送/接收...”按钮

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

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

于 2015-11-15T22:07:03.477 回答