我编码用于使用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
A QueryDef object does not have a RecordCount property; RecordCount是RecordSet或TableDef对象的属性。
因此,我建议按照以下内容将您的代码修改为某些内容:
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
以上内容未经测试,但有望使您朝正确的方向前进。