0

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.

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.

4

2 回答 2

19

在 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 = "Someone@somewhere.com"
oMail.Send
Set oMail = Nothing
Set oApp = Nothing


End Sub
于 2013-07-31T16:10:04.627 回答
7

这是我在我的一个数据库中使用的电子邮件代码。我只是为要发送给的人、抄送、主题和正文设置了变量。然后您只需使用 DoCmd.SendObject 命令。我还在正文之后将其设置为“True”,以便您可以在消息自动发送之前对其进行编辑。

Public Function SendEmail2()

Dim varName As Variant          
Dim varCC As Variant            
Dim varSubject As Variant      
Dim varBody As Variant          

varName = "james@yahoo.com"
varCC = "billy@gmail.com, joe@yahoo.com"
'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
于 2013-07-31T15:00:44.643 回答