vba Outlook关闭时如何发送邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14236696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send mail when outlook is closed
提问by SaiKiran Mandhala
i have following lines of code. It works fine when outlook is opened but i want it to work even though outlook is closed. I kept the code in command button click event.
我有以下几行代码。打开 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 = "[email protected]"
.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
I tried it with both GetObject and CreateObject methods. If i execute this code after closing outlook it's not showing any error but it's not sending any mail.
我用 GetObject 和 CreateObject 方法进行了尝试。如果我在关闭 Outlook 后执行此代码,它不会显示任何错误,但不会发送任何邮件。
The following lines of code sending the mails but they are queuing in the outlook's outbox. when user opens outlook then only they are moving out from outbox.
以下代码行发送邮件,但它们在 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 = "[email protected]"
.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
采纳答案by user2481205
You can use shell commands to actually open outlook before sending a mail.Precisely being
您可以使用 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
keep this in a separate module and call the module from the code where you are sending the mail.The part i am trying to work on is how to hide this so that activation is still with excel
将其保存在一个单独的模块中,并从您发送邮件的代码中调用该模块。我正在尝试处理的部分是如何隐藏它以便激活仍然使用 excel
回答by sanitycheck
For Outlook 2013, this is an issue with Outlook settings, not the VBA code.
对于 Outlook 2013,这是 Outlook 设置的问题,而不是 VBA 代码。
Open OUTLOOK
Go To FILE -> OPTIONS -> ADVANCED
Scroll to 'Send and Receive' heading and click 'Send/Receive…' button
Under 'Setting for group 'All Accounts' ', ensure that 'Perform an automatic send/receive when exiting' is CHECKED
打开展望
转到文件 -> 选项 -> 高级
滚动到“发送和接收”标题,然后单击“发送/接收...”按钮
在“组‘所有帐户’的设置”下,确保选中“退出时执行自动发送/接收”
This ensures all items in the OUTLOOK 'Outbox' are sent when Outlook closes. This fixed the issue for me. Likely similar for other versions of Outlook.
这可确保在 Outlook 关闭时发送 OUTLOOK“发件箱”中的所有项目。这为我解决了这个问题。其他版本的 Outlook 可能类似。