在电子邮件中添加附件

时间:2019-06-04 09:24:27

标签: excel vba outlook

我想创建一个按钮,以pdf格式保存工作表,将其附加到新邮件并发送。 我可以创建pdf,将pdf保存在我的桌面上,创建并通过电子邮件发送。但PDF从未附加。我要去哪里错了?

Dim pdfName As String
   pdfName = PONumberLabel.Caption  ' add PO number on label to a variable

     ' create pdf and save to desktop

       ChDir "C:\Users\roanderson\Desktop" ' selects directory to save
       Sheet4.ExportAsFixedFormat _
        Type:=xlTypePDF, _
        OpenAfterPublish:=True, _              
        Quality:=xlQualityStandard, _
        Filename:="C:\Users\roanderson\Desktop\" & pdfName  ' directory put t

    ' sending email for approval
    Dim xOutApp As Object
    Dim xOutMail As Object
    Dim xMailBody As String
    On Error Resume Next
    Set xOutApp = CreateObject("Outlook.Application")
    Set xOutMail = xOutApp.CreateItem(0)
    xMailBody = "Please approve PO Number" & " " & PONumber & vbNewLine & vbNewLine & _
              "Cost Centre:" & "   " & costcentre & vbNewLine & _
              "Description:" & "   " & description & vbNewLine & _
              "Currency:" & "   " & POCurrency & vbNewLine & _
              " Total:" & "   " & total
                  On Error Resume Next
    With xOutMail
        .To = "Ross.anderson@work.com"
        .CC = ""
        .BCC = ""
        .Subject = "PO Number " & PONumber & " " & "Approval"
        .Body = xMailBody
        .Display   'or use .Send
        .Attachments.Add "C:\Users\roanderson\Desktop\" & pdfName
        .VotingOptions = "Accept;Reject"




    End With
    On Error GoTo 0
    Set xOutMail = Nothing
    Set xOutApp = Nothing

除了将pdf附加到电子邮件之外,所有工作都可以。

1 个答案:

答案 0 :(得分:0)

更改声明

.Attachments.Add "C:\Users\roanderson\Desktop\" & pdfName

收件人

.Attachments.Add "C:\Users\roanderson\Desktop\" & pdfName & ".pdf"

似乎pdfname的扩展名不全。