我有以下代码行。当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
答案 0 :(得分: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
答案 1 :(得分:0)
对于Outlook 2013,这是Outlook设置的问题,而不是VBA代码。
打开OUTLOOK
转到文件 - &gt;选项 - &gt;高级
滚动至“发送和接收”标题,然后点击“发送/接收...”按钮
在“为群组设置所有帐户”下,确保“执行” 退出'已检查
这可确保在Outlook关闭时发送OUTLOOK“发件箱”中的所有项目。这为我解决了这个问题。其他版本的Outlook可能类似。