首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MS Access VBA发送带有PDF附件的电子邮件

MS Access VBA发送带有PDF附件的电子邮件
EN

Stack Overflow用户
提问于 2017-12-07 06:27:59
回答 1查看 4.4K关注 0票数 1

我正在尝试导出一个报告,从一个Access数据库到多个电子邮件地址使用一个表和我生成的报告。下面是我用来完成这个任务的代码。

代码语言:javascript
运行
复制
Function EmailNotification()
On Error GoTo Err_EmailNotification
    Dim olApp As Object
    Dim olMail As Object
    Set olApp = CreateObject("Outlook.Application")
    Set olMail = olApp.CreateItem(olMailItem)
    Dim EmailList As String
    Dim EmailList2 As String
    Dim objOutlookRecip As Object
    Dim objOutlookRecip2 As Object
    Dim objOutlookAttach As Object
    Const TERMINAL_QUERY = "SELECT EMail " & _
                          " FROM [EmailList] " & _
                          " ORDER BY Email;"

    Dim dbs As DAO.Database
    Dim rst1 As DAO.Recordset
    DoCmd.OutputTo acOutputReport, "CarryIn_Email", "PDFFormat(*.pdf)", "Q:\2017\Big E Transportation\Accounting\Advanced Auto\Projects Summary Report.PDF", False, " , acExportQualityPrint"
        Set dbs = CurrentDb()
        Set rst1 = dbs.OpenRecordset(TERMINAL_QUERY)
        With rst1
            .MoveFirst
            .MoveLast
            .MoveFirst
            rstX = rst1.RecordCount
            If Not (.EOF And .BOF) Then
                .MoveFirst
                Do Until .EOF
                    Set olApp = CreateObject("Outlook.Application")
                    Set olMail = olApp.CreateItem(olMailItem)
                    With olMail
                        Set objOutlookRecip = .Recipients.Add(rst1!Email)
                        objOutlookRecip.Type = olTo
                        .Subject = "Carry Ins"
                        SETOBJOUTLOOKATTACH = .Attachments.Add("Q:\2017\Big E Transportation\Accounting\Advanced Auto\Projects Summary Report.PDF")
                        .Send
                    End With
                    .MoveNext
                Loop
            End If
        End With

Exit_EmailNotification:
    Exit Function

Err_EmailNotification:
    MsgBox Error$
    Resume Exit_EmailNotification

End Function

我的问题是,这段代码不是导出附加了PDF的电子邮件,而是导出了附件为电子邮件的电子邮件。

我想要此代码导出带有PDF附件的电子邮件,而不是带有电子邮件作为附件的电子邮件。

EN

回答 1

Stack Overflow用户

发布于 2017-12-20 23:10:03

函数是用于计算并返回结果的过程。上面的函数没有返回任何内容。

子例程是通过进程中的步骤运行而不返回结果的过程。

下面提供的代码应该可以满足您正在尝试实现的目标:

代码语言:javascript
运行
复制
Public Sub EmailNotification()
Dim olApp                           As Object
Dim olMail                          As Object
Dim strExport, strList              As String
Dim rst1                            As DAO.Recordset

Const TERMINAL_QUERY = "SELECT EMail " & _
                       "FROM [EmailList] " & _
                       "ORDER BY Email;"

On Error GoTo ErrorH
'Varibale to update one location for entire code
strExport = "Q:\2017\Big E Transportation\Accounting\Advanced Auto\Projects Summary Report.PDF"
'Ensures strList is empty for below check
strList = Empty
'Outputs the report to PDF using strExport variable
DoCmd.OutputTo acOutputReport, "CarryIn_Email", "PDFFormat(*.pdf)", strExport, False, " , acExportQualityPrint"
'Opens the recordset containing email addresses within const query above
Set rst1 = CurrentDb.OpenRecordset(TERMINAL_QUERY)
'ensure the recordset is fully loaded
rst1.MoveLast
rst1.MoveFirst
'loop to acquire email addresses from query statement, adding ";" to separate each email address
Do While Not rst1.EOF
    If strList = Empty Then
        strList = rst1![Email]
    Else
        strList = strList & "; "
    End If
    rst1.MoveNext
Loop
'Closes recordset and frees object in memory
rst.Close
Set rst = Nothing
'Creates the memory for email objects
Set olApp = CreateObject("Outlook.Application")
Set olMail = olApp.CreateItem(olMailItem)
'Generates email information
With olMail
    'olFormatPlain is easier to type in an email with, my opinion only, this line is not needed
    .BodyFormat = olFormatPlain
    'Who the email is going to, using the strList created during loop above
    .To = strList
    .CC = "" 'Change if you want a CC
    .BCC = "" 'Change is you want a BCC
    .Subject = "Carry Ins"
    .Body = "" 'Change to what ever you want the body of the email to say
    'Attaches the exported file using the variable created at beginning
    .Attachments.Add = strExport
    .Display 'Use for testing purposes only, note out for live runs
    '.Send 'Use for live purposes only, note out for test runs
End With

'Frees email objects stored in memory
Set olMail = Nothing
Set olApp = Nothing

EndCode:
'Ensures all objects are free from memory
If Not rst1 Is Nothing Then
    rst1.Close
    Set rst1 = Nothing
End If
If Not olApp Is Nothing Then
    Set olMail = Nothing
    Set olApp = Nothing
End If
Exit Sub

'Error handler to display error infor in message box, resumes end code
'Change is you want/need this to handle specific error numbers
ErrorH:
MsgBox Err.Number & " - " & Err.Description
Resume EndCode
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47684528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档