将PDF添加到Outook

时间:2018-01-19 16:22:47

标签: vba pdf outlook

你可以帮我解决这个问题,我是一个初学者试图解决它。 我正在从特定工作表(第一页)创建PDF文件,我想将其添加到带有CC地址的电子邮件中。

Sub zapisz()

Dim ThisFile As String
Dim OutApp As Object
Dim OutMail As Object

ThisFile = Range("b8").Value & " " & Range("b9").Value & " " & Range("g8").Value & " " & Range("h8").Value

ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
    ThisFile, Quality:= _
    xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, _
    From:=1, To:=1, OpenAfterPublish:=False


Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
    .To = ""
    .CC = "monika@xx.pl"
    .BCC = ""
    .Subject = "Oferta xxx"
    .Body = "Szanowni Państwo, w załączniku przesyłam ofertę."
    .Attachments.Add (ThisFile & "*" & ".pdf")
    .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

With Application
    .ScreenUpdating = True
    .EnableEvents = True
 End With

 End Sub`

1 个答案:

答案 0 :(得分:0)

使用后期绑定很方便,在StackOverflow中提问时,这确实是一个很好的做法!一般来说,试试这样:

Sub zapisz()

        Dim ThisFile As String
        Dim OutApp As Object
        Dim OutMail As Object

        ThisFile = Range("b8") 'include the rest of your range here for the name

        ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
            ThisFile, Quality:= _
            xlQualityStandard, IncludeDocProperties:=False, IgnorePrintAreas:=False, _
            From:=1, To:=1, OpenAfterPublish:=False        

        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
        With OutMail
            .To = ""
            .CC = "monika@xx.pl"
            .BCC = ""
            .Subject = "Oferta xxx"
            .Body = "Szanowni Panstwo, w zalaczniku przesylam oferte."
            .Attachments.Add (ThisWorkbook.Path & "\" & ThisFile & ".pdf")
            .Display
        End With
        'more of your code here
End Sub

确保将初始Excel文件保存在某处(PC中的任何位置都可以)。保存后,您可以使用ThisWorkbook.Path来添加附件。 我已经查了ThisFile = Range("B8"),但你可以随时修复它。

您可以考虑删除On Error GoTo 0,只有在您使用On Error Resume NextOn Error GoTo zapisz_Error之前的错误捕获器时才需要它。