MS Access VBA:通过 Outlook 发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17973549/
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
MS Access VBA: Sending an email through Outlook
提问by John Smith
How can I send an email through an account using MS Access VBA? I know this question is vague but it's so hard to find the relevant information online that isn't outdated in some way.
如何使用 MS Access VBA 通过帐户发送电子邮件?我知道这个问题很模糊,但很难在网上找到不以某种方式过时的相关信息。
EDIT: I don't mean to be rude to those who are answering, but I am using MS Access. I cannot write the actual code in Outlook VBA.
编辑:我并不是要对那些回答的人粗鲁,但我正在使用 MS Access。我无法在 Outlook VBA 中编写实际代码。
回答by Sorceri
Add a reference to the Outlook object model in the Visual Basic editor. Then you can use the code below to send an email using outlook.
在 Visual Basic 编辑器中添加对 Outlook 对象模型的引用。然后,您可以使用下面的代码使用 Outlook 发送电子邮件。
Sub sendOutlookEmail()
Dim oApp As Outlook.Application
Dim oMail As MailItem
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(olMailItem)
oMail.Body = "Body of the email"
oMail.Subject = "Test Subject"
oMail.To = "[email protected]"
oMail.Send
Set oMail = Nothing
Set oApp = Nothing
End Sub
回答by nedstark179
Here is email code I used in one of my databases. I just made variables for the person I wanted to send it to, CC, subject, and the body. Then you just use the DoCmd.SendObject command. I also set it to "True" after the body so you can edit the message before it automatically sends.
这是我在我的一个数据库中使用的电子邮件代码。我只是为我想发送它的人、抄送、主题和正文制作了变量。然后您只需使用 DoCmd.SendObject 命令。我还在正文之后将其设置为“True”,以便您可以在消息自动发送之前对其进行编辑。
Public Function SendEmail2()
Dim varName As Variant
Dim varCC As Variant
Dim varSubject As Variant
Dim varBody As Variant
varName = "[email protected]"
varCC = "[email protected], [email protected]"
'separate each email by a ','
varSubject = "Hello"
'Email subject
varBody = "Let's get ice cream this week"
'Body of the email
DoCmd.SendObject , , , varName, varCC, , varSubject, varBody, True, False
'Send email command. The True after "varBody" allows user to edit email before sending.
'The False at the end will not send it as a Template File
End Function