【问题标题】:Pass an email address to the SendObject method in VBA将电子邮件地址传递给 VBA 中的 SendObject 方法
【发布时间】:2013-01-06 08:33:09
【问题描述】:

我想从我的表“GetContact_TempTbl”中获取电子邮件地址并将报告发送到该电子邮件地址。此电子邮件将根据收到的公司而更改。我目前提取相关电子邮件地址并将其存储在临时表中. 我目前收到 Object Required 错误。

非常感谢您的建议。

Dim db As Database
Dim rs As Recordset
Dim stRecipients As String
Dim stDocName As String

Set db = CurrentDb()
Set rs = db.OpenRecordset("GetContact_TempTbl")
Set stRecipients = rs.Fields("Contact_Email")
stDocName = "License CODs"
stRecipietns = stRecipients

DoCmd.SendObject acReport, stDocName, acFormatPDF, stRecipients, , , "Thank You for your purchase"

【问题讨论】:

    标签: email vba ms-access-2007


    【解决方案1】:

    如果您的记录集为您要发送电子邮件的每个收件人保留一行,请遍历记录集以收集它们,而不是仅读取第一行的收件人。

    Const stDocName As String = "License CODs"
    Dim db As DAO.database
    Dim rs As DAO.Recordset
    Dim stRecipients As String
    
    Set db = CurrentDb()
    Set rs = db.OpenRecordset("GetContact_TempTbl")
    With rs
        Do While Not .EOF
            stRecipients = stRecipients & ";" & !Contact_Email
            .MoveNext
        Loop
        .Close
    End With
    
    If Len(stRecipients) > 0 Then
        ' discard leading ";"
        stRecipients = Mid(stRecipients, 2)
        DoCmd.SendObject acReport, stDocName, acFormatPDF, _
            stRecipients, , , "Thank You for your purchase"
    Else
        MsgBox "No recipients to email!"
    End If
    
    Set rs = Nothing
    Set db = Nothing
    

    但是,如果我的解释不正确,并且记录集总是包含只有一个 Contact_Email 的单行,那么您甚至不需要记录集。您可以简单地使用DLookup 检索Contact_Email

    stRecipients = Nz(DLookup("Contact_Email", "GetContact_TempTbl"), "")
    

    【讨论】:

      【解决方案2】:

      您应该只将set 用于对象,而不是字符串:

      Dim db As Database
      Dim rs As Recordset
      Dim stRecipients As String
      Dim stDocName As String
      
      Set db = CurrentDb()
      Set rs = db.OpenRecordset("GetContact_TempTbl")
      ''This is not a field object, it is a string
      stRecipients = rs.Fields("Contact_Email")
      stDocName = "License CODs"
      stRecipietns = stRecipients
      
      DoCmd.SendObject acReport, stDocName, acFormatPDF, _
         stRecipients, , , "Thank You for your purchase"
      

      如果你说出你是如何创建临时表的,这可能会更容易。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        • 2011-07-13
        • 2020-09-28
        • 2021-04-20
        • 1970-01-01
        • 2014-12-12
        • 1970-01-01
        相关资源
        最近更新 更多