Excel VBA -如何从Excel中的列表创建PDF并通过电子邮件发送

q1qsirdb  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(202)

这里有两个部分涉及Excel VBA和从100多个项目的列表中创建PDF,然后通过电子邮件发送列表中的PDF。我很坚韧弄清楚如何通过电子邮件发送我创建的PDF。不确定这是否是一个单独的VBA脚本,或者我是否可以将电子邮件代码包含在现有的脚本中。
1.从Excel中的“列表”创建PDF(解决了这一部分,代码如下)
1.通过电子邮件发送“列表”中的PDF(我需要这部分的帮助)
注意:列表是唯一ID号的范围,其具有对应的电子邮件地址。
从列表代码创建PDF(我现在正在将PDF保存到文件夹中):

Sub SavePDFsFromList()

'Declare the Variables
Dim ws As Worksheet
Dim rngID As Range
Dim rngListStart As Range
Dim rowsCount As Long
Dim i As Long
Dim pdfFilePath As String
Dim tempPDFFilePath As String

'Stop the screen updating while running
Application.ScreenUpdating = False

'Reference the tab the pdf will be created from
Set ws = ActiveWorkbook.Sheets("Statement")

'Cell that generates information on each pdf
Set rngID = ws.Range("A1")

'Reference the start of the  ID List
Set rngListStart = ws.Range("M4")

'Count the rows in the ID List
rowsCount = rngListStart.CurrentRegion.Rows.Count - 1

'Create the PDF File Name
pdfFilePath = "C:\Test Folder\PDF Export\Example - [ID].pdf"

For i = 1 To rowsCount

    'Change the current ID
    rngID.Value = rngListStart.Offset(i - 1, 0).Value

    'Replace [ID] with ID Value
    tempPDFFilePath = Replace(pdfFilePath, "[ID]", rngID.Value)

    'Create the PDF
    ws.ExportAsFixedFormat Type:=xlTypePDF, _
        Filename:=tempPDFFilePath

Next i

'Restart the screen updating
Application.ScreenUpdating = True

End Sub

谢谢!

xqk2d5yq

xqk2d5yq1#

尝试

Sub SavePDFsFromList()

    'Declare the Variables
    Dim ws As Worksheet
    Dim rngID As Range
    Dim rngListStart As Range
    Dim rowsCount As Long
    Dim i As Long
    Dim pdfFilePath As String
    Dim tempPDFFilePath As String

    'Stop the screen updating while running
    Application.ScreenUpdating = False

    'Reference the tab the pdf will be created from
    Set ws = ActiveWorkbook.Sheets("Statement")

    'Cell that generates information on each pdf
    Set rngID = ws.Range("A1")

    'Reference the start of the  ID List
    Set rngListStart = ws.Range("M4")

    'Count the rows in the ID List
    rowsCount = rngListStart.CurrentRegion.Rows.Count - 1

    'Create the PDF File Name
    pdfFilePath = "C:\Test Folder\PDF Export\Example - [ID].pdf"

    For i = 1 To rowsCount

        'Change the current ID
        rngID.Value = rngListStart.Offset(i - 1, 0).Value

        'Replace [ID] with ID Value
        tempPDFFilePath = Replace(pdfFilePath, "[ID]", rngID.Value)

        'Create the PDF
        ws.ExportAsFixedFormat Type:=xlTypePDF, _
                               Filename:=tempPDFFilePath
        
        ' START-EMAIL EACH PDF TO EACH CORRESPONDING RECEPIENT IN COLUMN N
    
        With CreateObject("outlook.application").CreateItem(0)
            .To = ws.Cells(i + 3, "N").Value
            .Subject = "This is the subject"
            .Body = "This is the body"
            .Attachments.Add tempPDFFilePath
            .Display 'COMMENT THIS LINE THEN UNCOMMENT .SEND LINE AFTER TESTING
            '.Send
        End With
    
        'END-EMAIL EACH PDF TO EACH CORRESPONDING RECEPIENT IN COLUMN N

    Next i

    'Restart the screen updating
    Application.ScreenUpdating = True

结束接头

相关问题