简体   繁体   English

MS Access VBA 密码发送一个 Outlook email

[英]MS Access VBA code to send an Outlook email

I am trying to get Access to send a simple email programatically.我正在尝试让 Access 以编程方式发送一个简单的 email。 I have added the Outlook 16.0 reference.我添加了 Outlook 16.0 参考。 Below is the code.下面是代码。 When it gets to the With oMail part, it returns an error: "Application-defined of object-defined error."当它到达 With oMail 部分时,它返回一个错误:“应用程序定义的对象定义的错误。”

It errors on the.ReplyRecipients.Add line.它在.ReplyRecipients.Add 行上出错。 If I comment out that line, then it errors on the.Send line.如果我注释掉该行,那么它会在 .Send 行上出错。

Note I am running the TestSend() sub to activate the SendEmailOutlook sub.注意我正在运行 TestSend() sub 来激活 SendEmailOutlook sub。

Sub TestSend()
    Call SendEmailOutlook("myemail@email.com", "Test message", "Test message.")
End Sub

Public Sub SendEmailOutlook(strTo As String, strSubject As String, strBody As String)
    
    On Error GoTo SendEmailOutlookErr
    
    Dim strEmail As String
    Dim strMsg As String
    Dim oLook As Object
    Dim oMail As Object
    
    Set oLook = CreateObject("Outlook.Application")
    Set oMail = oLook.createitem(0)
    With oMail
        .ReplyRecipients.Add "myemail@email.com"
        .to = strTo
        .htmlbody = strBody
        .Subject = strSubject
        .Send
    End With
    Set oMail = Nothing
    Set oLook = Nothing
    Exit Sub
    
SendEmailOutlookErrExit:
        Exit Sub
    
SendEmailOutlookErr:
        MsgBox Err.Description, vbOKOnly, Err.Source & ":" & Err.Number
        Resume SendEmailOutlookErrExit
End Sub

In the code the late binding technology is used, so the COM reference is optional.代码中使用了后期绑定技术,所以COM引用是可选的。 But if you have already added the Outlook COM refence you may declare all Outlook objects instead of just having the object in the declaration.但是,如果您已经添加了 Outlook COM 引用,您可以声明所有 Outlook 对象,而不是仅在声明中包含 object。 It can help.它可以提供帮助。 Read more about the late and early binding in the Using early binding and late binding in Automation article. 在自动化中使用早期绑定和后期绑定一文中阅读有关后期绑定和早期绑定的更多信息。

Also the following line of code contains multiple property and method calls :此外,以下代码行包含多个属性和方法调用

With oMail
  .ReplyRecipients.Add "myemail@email.com"

The code is valid.代码有效。 But it makes sense to declare each property or method on a separate line of code, so you could easily find the faulting one - where exactly the error occurs.但是在单独的代码行中声明每个属性或方法是有意义的,这样您就可以轻松找到错误的代码 - 错误发生的确切位置。

The MailItem.ReplyRecipients property returns a Recipients collection that represents all the reply recipient objects for the Outlook item. MailItem.ReplyRecipients属性返回一个Recipients集合,该集合表示 Outlook 项目的所有回复收件人对象。 Use the Add method to create a new Recipient object and add it to the Recipients object. The Type property of a new Recipient object is set to the default for the associated AppointmentItem , JournalItem , MailItem , or TaskItem object and must be reset to indicate another recipient type.使用Add方法创建一个新的Recipient object 并将其添加到Recipients object。新 Recipient object 的Type属性设置为关联的AppointmentItemJournalItemMailItemTaskItem object 的默认值,并且必须重置以指示另一个收件人类型。

Set myItem = Application.CreateItem(olMailItem)  
Set myRecipient = myItem.Recipients.Add ("Jon Grande")  
myRecipient.Type = olCC

Another aspect is how Outlook has been configured to trust applications on a client computer, an application that uses the Outlook object model to access certain data or execute certain actions can invoke security warnings or throw errors when Outlook is automated without any UI.另一个方面是 Outlook 如何配置为信任客户端计算机上的应用程序,使用 Outlook object model 访问某些数据或执行某些操作的应用程序可以调用安全警告或在 Outlook 没有任何 UI 的情况下自动化时引发错误。 Depending on the type of information or action that the program was attempting to access or execute, there are three different security prompts that applications can invoke through the Object Model Guard: the address book warning, send message warning, and execute action warning.根据程序试图访问或执行的信息或操作的类型,应用程序可以通过 Object Model Guard 调用三种不同的安全提示:地址簿警告、发送消息警告和执行操作警告。 Read more about that in the Outlook Object Model Security Warnings article.Outlook Object Model 安全警告一文中阅读更多相关信息。

The error message "Application-defined or object-defined error" suggests that there is an issue with the object or variable being referred to.错误消息“应用程序定义或对象定义的错误”表明 object 或引用的变量存在问题。 In this case, it is likely that the issue is with the "ReplyRecipients.Add" line.在这种情况下,问题很可能与“ReplyRecipients.Add”行有关。

The "ReplyRecipients" property is used to add recipients to the "To" field of a reply message, but in this case, you're trying to add recipients to a new mail item. “ReplyRecipients”属性用于将收件人添加到回复消息的“收件人”字段,但在本例中,您正尝试将收件人添加到新邮件项目。 Instead of using "ReplyRecipients.Add," try using the "Recipients.Add" property, which adds recipients to the "To" field of a new mail item.不要使用“ReplyRecipients.Add”,而是尝试使用“Recipients.Add”属性,它将收件人添加到新邮件项的“收件人”字段中。

Also, "oLook.createitem(0)" is used to create a new mail item, you can use "oLook.CreateItem(0)" instead.此外,“oLook.createitem(0)”用于创建新邮件项目,您可以使用“oLook.CreateItem(0)”代替。

Try this:尝试这个:

Public Sub SendEmailOutlook(strTo As String, strSubject As String, strBody As String)

On Error GoTo SendEmailOutlookErr

Dim oLook As Object
Dim oMail As Object

Set oLook = CreateObject("Outlook.Application")
Set oMail = oLook.CreateItem(0)
With oMail
    .Recipients.Add strTo
    .HTMLBody = strBody
    .Subject = strSubject
    .Send
End With
Set oMail = Nothing
Set oLook = Nothing
Exit Sub

SendEmailOutlookErrExit:
Exit Sub

SendEmailOutlookErr:
MsgBox Err.Description, vbOKOnly, Err.Source & ":" & Err.Number
Resume SendEmailOutlookErrExit
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM