【问题标题】:Sending a string of attachments from a table从表中发送一串附件
【发布时间】:2020-06-20 06:45:00
【问题描述】:

我有一个数据库,其中存储了我客户的医疗申报表。

表单存储在外部文件夹 (c:\...\medical form) 中,并命名为 (DDMMYYYY Fname Lname)。这个文件名(不是strpath)数据存储在[med forms]中。

在父表单 (courses) 上,我尝试在子表单 [courses customer subform] 上放置一个循环通过 [med forms] 的按钮,并将它们附加到一封电子邮件,然后发送给我。

我无法循环浏览表单并连接 strpathstrfile 以附加这些文档。

Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Dim strpath As String
Dim strfile As String
Dim varSubject As Variant
Dim varGroup As Variant
Dim varBody As Variant
Dim stratt As String
strpath = "C:\...\Medical forms"
varSubject = "Med forms " & (Me.[Title]) & (Me.[Start])
varBody = "email body TBC"
    With Me.[courses customer subform].Form.RecordsetClone
    If (.RecordCount) Then
        .MoveFirst
        Do Until .EOF
            If Len(![Med form]) Then
                stratt = stratt & strpath & ![Med form]
            End If
        .MoveNext
        Loop
        If Len(strEmail) Then

    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)

    With MailOutLook
        .BodyFormat = olFormatRichText
        .To = "info@myemail.co.uk"
        
        .Subject = varSubject
        .HTMLBody = varBody
        .Attachments.Add (strpath & strfile)
        .Display
    End With
End sub

【问题讨论】:

  • 请不要在命名对象时使用空格。以后会省去很多麻烦:)
  • 平心而论,我在学习使用访问权限时创建了这个数据库,我绝对知道现在供将来参考:D

标签: vba ms-access outlook


【解决方案1】:

由于您已经在使用后期绑定 (CreateObject("Outlook.Application")),我建议您在其余的 Sub 中使用后期绑定(避免添加库引用),并建议以下内容:

Sub EmailForms()
    Dim strPth As String: strPth = "C:\...\Medical forms\"
    Dim strSub As String: strSub = "Med forms " & Me.[Title] & Me.[Start]
    Dim strBdy As String: strBdy = "email body TBC"

    Dim rsRst As DAO.Recordset
    Set rsRst = Me.[courses customer subform].Form.RecordsetClone
    If Not rsRst.BOF And Not rsRst.EOF Then
        rsRst.MoveFirst

        Dim olApp As Object
        Set olApp = CreateObject("Outlook.Application")

        With olApp.CreateItem(0) 'olMailItem
            .BodyFormat = 3 'olFormatRichText
            .to = "info@myemail.co.uk"
            .Subject = strSub
            .HTMLBody = strBdy

            With .Attachments
                Do Until rsRst.EOF
                    If rsRst![Med form] <> vbNullString Then
                        .Add strPth & rsRst![Med form]
                    End If
                    rsRst.MoveNext
                Loop
            End With

            .Display
        End With
    End If
    rsRst.Close
End Sub

【讨论】:

  • 检查Not rs.EOF可能会失败,如果有人做了奇怪的事情,比如rs.MoveLastandrs.MoveNext之后(然后rs.EOF为真)所以最好检查Not rs.BOF And Not rs.EOF
  • 非常感谢您的快速响应,我在这方面不是最好的,只为我自己的小项目真正做到这一点,所以它是一个巨大的帮助!我还添加了更多内容,例如附加课程注册。再次感谢您的帮助。
猜你喜欢
  • 2015-07-27
  • 2019-03-07
  • 1970-01-01
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 2020-07-15
  • 2011-06-25
  • 1970-01-01
相关资源
最近更新 更多