通过 Outlook 从 Access VBA 发送自动电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19256504/
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
Sending Automated Email from Access VBA through Outlook
提问by Grant
Ok, so I've been putting band aids on top of band aids on a simple snippet of code to get an email to send. What I've been trying to do is get an email to send through outlook. My first issue was runtime object define 287
at the following line:
好的,所以我一直在一个简单的代码片段上将创可贴放在创可贴之上,以获取要发送的电子邮件。我一直在尝试做的是通过 Outlook 发送一封电子邮件。我的第一个问题是runtime object define 287
在以下行:
Set appOutlookRec = appOutlookMsg.Recipients.Add
so to counter that I added:
所以为了反驳我补充说:
Set objNS = appOutlook.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
And that stopped that. Now the email will form and I can use .display
to see my email, but when I try and use .send
the instance of Outlook closes before the email is actually sent.
这阻止了它。现在电子邮件将形成,我可以.display
用来查看我的电子邮件,但是当我尝试使用.send
Outlook 实例时,它会在电子邮件实际发送之前关闭。
To counter that I force outlook to open, but I would like to check if an instance is already open. To open the inbox I use:
为了解决这个问题,我强制打开 Outlook,但我想检查一个实例是否已经打开。要打开收件箱,我使用:
Set appOutlook = CreateObject("Outlook.Application")
Set objNS = appOutlook.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
olFolder.Display
Am I over complicating the automation process? Or am I on the right path and someone can help checking if an instance of Outlook is open in the explorer window?
我是否使自动化过程过于复杂?或者我是否在正确的道路上,有人可以帮助检查 Outlook 实例是否在资源管理器窗口中打开?
Thanks
谢谢
Update
更新
Const olMailItem = 0
Const olTo = 1
Const olCC = 2
Const olFolderInbox = 6
Dim appOutlook As Object
Dim appOutlookMsg As Object
Dim appOutlookRec As Object
Dim objNS As Object
Dim olFolder As Object
Set appOutlook = CreateObject("Outlook.Application")
Set objNS = appOutlook.GetNamespace("MAPI")
Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
olFolder.Display
'Create a new empty email message
Set appOutlookMsg = appOutlook.CreateItem(olMailItem)
With appOutlookMsg
Set appOutlookRec = appOutlookMsg.Recipients.Add("[email protected]")
appOutlookRec.Type = olTo
.Subject = "Testing Closed Tickets"
.Body = "This is just a test."
'.Display
.Send
End With
Set appOutlookMsg = Nothing
Set appOutlook = Nothing
Set appOutlookRec = Nothing
回答by David Zemens
I've encountered something similar before and I think usually you can avoid the error by saving the email prior to sending:
我以前遇到过类似的事情,我认为通常您可以通过在发送之前保存电子邮件来避免错误:
appOutlookMsg.Save
appOutlookMsg.Send