【问题标题】:Send Email from Outlook using MS Access VBA使用 MS Access VBA 从 Outlook 发送电子邮件
【发布时间】:2020-05-16 08:01:36
【问题描述】:

我在 MS Access 报告中有以下 Visual Basic for Applications 代码。

我在运行报告时打开了 Outlook 应用程序。

如果 MS Access“评论”表中“序列号”列中的值等于“Parts.mdb”数据库中的 3276,我想发送电子邮件。

代码没有给出任何错误,但我没有收到电子邮件。

完整代码:

Option Compare Database

Option Explicit
Dim DB As Database
Dim wrkJet As Workspace
Dim CoeReportsDB As Database
Dim CommentsRS As Recordset
Dim oApp As Outlook.Application
Dim oMail As MailItem

Sub SendEmail()

    Set DB = CurrentDb()
    Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
    Set CoeReportsDB = wrkJet.OpenDatabase("T:\Parts.mdb")
    
    Set CommentsRS = CoeReportsDB.OpenRecordset("Comments", dbOpenTable)
    CommentsRS.index = "Sequence Number"
    CommentsRS.Seek "=", 3276

    If Not CommentsRS.NoMatch Then
        Set oApp = CreateObject("Outlook.application")
        Set oMail = oApp.CreateItem(olMailItem)
        oMail.Body = "Sequence Number Out of Range"
        oMail.Subject = "Perform Sequence Number Reset."
        oMail.To = "someone@somewhere.com"
        oMail.Send
        Set oMail = Nothing
        Set oApp = Nothing
    End If
    
ShutDown:
    CommentsRS.Close
    Set DB = Nothing
    
End Sub

【问题讨论】:

标签: vba ms-access outlook


【解决方案1】:

The Recordset.Seek method不返回任何东西。相反,它会更改Recordset 对象的Index 属性,如果Seek 找不到任何匹配结果,则更改NoMatch 属性。

该行的正确写法如下:

CommentsRS.Seek "=", 3276
If Not CommentsRS.NoMatch Then

【讨论】:

  • 感谢您的回答!我按照你说的做了更改,但我仍然没有收到电子邮件。代码没有给出任何错误。
  • 您是否检查过if 语句正在被验证? (例如,通过在 then 参数中放置 debug.print "Yes"
  • @techindustry 请不要编辑您的问题以使现有答案无效 - 如果这样做会使其他有相同问题的人更难找到解决方案!要么添加附录,在答案的评论中查询(就像你一样),或者创建一个新问题。
猜你喜欢
  • 2021-11-21
  • 2011-09-28
  • 2021-05-11
  • 2020-05-20
  • 2013-08-01
  • 2013-10-15
  • 1970-01-01
  • 2019-08-25
相关资源
最近更新 更多