Добавление PDF в Outook

Обновить

April 2019

Просмотры

31 раз

1

Можете ли вы помочь мне с этим кодом, я новичок пытается решить это. Я создаю 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 = "[email protected]"
    .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`
Ada

1 ответы

0

Good job for using late binding, this is really a good practice, when asking questions in StackOverflow! In general, give it a try like this:

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 = "[email protected]"
            .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

Make sure that the initial Excel file is saved somewhere (anywhere in your PC would work). Once it is saved, you would be able to use ThisWorkbook.Path, in order to add the attachment. I have chagned ThisFile = Range("B8"), but you can fix it back once you want.

You may consider removing On Error GoTo 0, it is only needed if you are using some error catcher like On Error Resume Next or On Error GoTo zapisz_Error before it.