Attach Active Sheet as custom named PDF to a new Email

04whitem

New Member
Joined
Oct 9, 2018
Messages
1
Hi,

This one has been bugging me for a while - I am hoping somebody will be able to help.

I have been using the following code:

Code:
Sub EmailPDFPaymentNotice()
Application.ScreenUpdating = False
  Dim IsCreated As Boolean
  Dim i As Long
  Dim PdfFile As String, Title As String
  Dim OutlApp As Object, signature As String
  Dim Filename As String
  ' Define Email subject line
  Title = Range("A4").Value
  ' Define PDF filename
  PdfFile = ActiveWorkbook.FullName
  Filename = Range("A4").Value
  i = InStrRev(PdfFile, ".")
  If i > 1 Then PdfFile = Left(PdfFile, i - 1)
  PdfFile = PdfFile & Filename & ".pdf"
  'PdfFile = FileName & ".pdf"
  ' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With
  ' Use already open Outlook if possible
  On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")
  If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
  End If
  OutlApp.Visible = True
  On Error GoTo 0
 Set OutlMail = OutlApp.CreateItem(0)
  ' Prepare e-mail with PDF attachment
  With OutlMail
    ' Prepare e-mail
    .Subject = Title
    .To = Range("M37").Value ' <-- Put email of the recipient here
   ' .CC = "..." ' <-- Put email of 'copy to' recipient here
    .Body = Range("D37").Value & "," & vbLf & vbLf _
          & "Please find attached Payment Notice Nr " & Range("O5").Value & " with reference to your works at " & Range("C6").Value & "." & vbLf & vbLf
          '& Application.DefaultSaveFormat & vbLf & vbLf
    .Attachments.Add PdfFile
    ' Try to send (display)
    On Error Resume Next
      .Display
      '.Send
    'Application.Visible = True
    'If Err Then
     ' MsgBox "E-mail was not sent", vbExclamation
    'Else
     ' MsgBox "E-mail successfully sent", vbInformation
    'End If
    'On Error GoTo 0
  End With
  ' Delete PDF file
  Kill PdfFile
  ' Quit Outlook if it was created by this code
 ' If IsCreated Then OutlApp.Quit
  ' Release the memory of object variable
 ' Set OutlApp = Nothing
Application.ScreenUpdating = True
MenuFull.Hide
End Sub

Which is an alteration to some code i found on Spreadsheet Guru.

I have managed in the most part to get it to work - except the name of the actual PDF document that gets attached to the email. I dont want this to be the Workbook name & then date in A4. I just want it to be named as per what is in Cell A4 (.pdf).

I have tried fiddling with it, but whenever i do, i just get a debug error, that highlights the .Attachments.Add PdfFile

I would really appreciate some assistance?

cheers
 
Last edited by a moderator:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top