VBA中的参考查询,用于发送多封电子邮件

问题描述 投票:1回答:1

我编码用于使用Access数据库通过Outlook发送多封电子邮件。

我想按标准搜索电子邮件查询,并使用这些过滤器电子邮件发送电子邮件,但我的VBA代码存在以下问题:

“未找到方法”的“ Query1.RecordCount

Private Sub outlook_Click()
Dim ooutlook As outlook.Application
Dim oEmailitem As outlook.MailItem
Dim rs As Recordset
Dim emaillist As String
Dim Query As String
Dim Query1 As QueryDef
If ooutlook Is Nothing Then
    Set ooutlook = New outlook.Application
End If
Set oEmailitem = ooutlook.CreateItem(olMailItem)
With oEmailitem
Query = "QryStudentAddressDetails"
        Set Query1 = CurrentDb.QueryDefs(Query)
        If Query1.RecordCount > 0 Then
        rs.MoveFirst
        Do Until rs.EOF
            If IsNull(rs![Email_Address]) Then
            rs.MoveNext
            Else
                emaillist = emaillist & rs![Email_Address] & ";"
                .To = emaillist
                rs.MoveNext
            End If
        Loop
        Else
        `enter code here`MsgBox "No one has Email Address!"
        End If
        Set rs = Nothing
    .CC = ""
    .Subject = "testing email"
    .Display
    End With
    Set oEmailitem = Nothing
    Set ooutlook = Nothing


End Sub
vba ms-access access-vba outlook-vba
1个回答
0
投票

A QueryDef object does not have a RecordCount property; RecordCountRecordSetTableDef对象的属性。

因此,我建议按照以下内容将您的代码修改为某些内容:

Private Sub outlook_Click()

    Dim cdb As DAO.Database
    Dim rst As DAO.Recordset
    Dim ola As Outlook.Application
    Dim olm As Outlook.MailItem
    Dim rcp As String

    Set cdb = CurrentDb
    Set rst = cdb.OpenRecordset("QryStudentAddressDetails")

    If Not rst.EOF Then
        rst.MoveFirst
        Do Until rst.EOF
            If Not IsNull(rst!Email_Address) Then
                rcp = rcp & rst!Email_Address & ";"
            End If
            rst.MoveNext
        Loop
    End If
    rst.Close

    If rcp <> vbNullString Then
        Set ola = New Outlook.Application
        Set olm = ola.CreateItem(olMailItem)
        With olm
            .to = rcp
            .Subject = "Testing Email"
            .Display
        End With
        Set olm = Nothing
        Set ola = Nothing
    End If
End Sub

以上内容未经测试,但有望使您朝正确的方向前进。

© www.soinside.com 2019 - 2024. All rights reserved.