Printing sheets to PDF and attaching to an email.

desnyder2001

New Member
Joined
Aug 4, 2017
Messages
12
FIRST: I am an extreme novice using Macros so if you choose to assist me please try and talk at my level (thanks).

CURRETLY: I found this macro a while back while searching and it does everything perfect for me. It attaches all my sheets to a single pdf and automatically emails to the person I need it to.

NEEDED: I now need it to print each sheet to its own PDF with the name of the sheet and attach all PDFs to the email.

Following is the code that is being used:

CODE:
Sub Email_ActiveSheet_As_PDF()


'Do not forget to change the email ID
'before running this code


Dim OlApp As Object
Dim NewMail As Object
Dim TempFilePath As String
Dim TempFileName As String
Dim FileFullPath As String


With Application
.ScreenUpdating = False
.EnableEvents = False
End With


' Temporary file path where pdf
' file will be saved before
' sending it in email by attaching it.


TempFilePath = Environ$("temp") & ""


' Now append a date and time stamp
' in your pdf file name. Naming convention
' can be changed based on your requirement.


TempFileName = "PDF DRAFT Invoices" & "-" & Format(Now, "dd-mmm-yy") & ".pdf"


'Complete path of the file where it is saved
FileFullPath = TempFilePath & TempFileName


'Now Export the Activesshet as PDF with the given File Name and path


On Error GoTo err
With ActiveWorkbook
.ExportAsFixedFormat _
Type:=xlTypePDF, _
FileName:=FileFullPath, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End With


'Now open a new mail


Set OlApp = CreateObject("Outlook.Application")
Set NewMail = OlApp.CreateItem(0)


On Error Resume Next
With NewMail
.To = "name@xxx.com"
.CC = "name@xxx.com"
.BCC = ""
.Subject = "Draft Invoices"
.Body = "Michelle, I have attached this months DRAFT invoices for your review and processing."
.Attachments.Add FileFullPath '--- full path of the pdf where it is saved
.Send 'or use .Display to show you the email before sending it.
End With
On Error GoTo 0


'Since mail has been sent with the attachment
'Now delete the pdf file from the temp folder


Kill FileFullPath


'set nothing to the objects created
Set NewMail = Nothing
Set OlApp = Nothing


'Now set the application properties back to true
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
MsgBox ("Make sure that Outlook is open and your email with attachment will be sent")

Exit Sub
err:
MsgBox err.Description


End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hi desnyder,

please use code tags when posting code (see in red below)

this should do the trick:
Code:
Sub Email_ActiveSheet_As_PDF()


    'Do not forget to change the email ID
    'before running this code
    
    
    Dim OlApp As Object
    Dim NewMail As Object
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim FileFullPath As String
    Dim wsWS As Worksheet
    
    
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    
    
    ' Temporary file path where pdf
    ' file will be saved before
    ' sending it in email by attaching it.
    
    
    TempFilePath = Environ$("temp") & ""
    
    
    
    
    'Now Export each sheet as PDF with the given File Name and path
    'loop through all the sheets
    
    On Error GoTo err
    For Each wsWS In ActiveWorkbook.Sheets
        With wsWS
            ' Create pdf name
            TempFileName = .Name & ".pdf"
            'Complete path of the file where it is saved
            FileFullPath = TempFilePath & TempFileName
            'create the .pdf
            .ExportAsFixedFormat _
                    Type:=xlTypePDF, _
                    Filename:=FileFullPath, _
                    Quality:=xlQualityStandard, _
                    IncludeDocProperties:=True, _
                    IgnorePrintAreas:=False, _
                    OpenAfterPublish:=False
        End With
    Next wsWS
    
    'Now open a new mail
    
    
    Set OlApp = CreateObject("Outlook.Application")
    Set NewMail = OlApp.CreateItem(0)
    
    
    On Error Resume Next
    With NewMail
        .To = "name@xxx.com"
        .CC = "name@xxx.com"
        .BCC = ""
        .Subject = "Draft Invoices"
        .Body = "Michelle, I have attached this months DRAFT invoices for your review and processing."
        '~~> *.* for all files
        TempFileName = Dir(TempFilePath & "*.*")

        Do While Len(TempFileName) > 0
            .Attachments.Add TempFilePath & TempFileName
            TempFileName = Dir
        Loop
        .Send 'or use .Display to show you the email before sending it.
    End With
    On Error GoTo 0
    
    
    'Since mail has been sent with the attachment
    'Now delete the pdf files from the temp folder
    
    
    Kill TempFilePath & "*.pdf"
    
    
    'set nothing to the objects created
    Set NewMail = Nothing
    Set OlApp = Nothing
    
    
    'Now set the application properties back to true
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    MsgBox ("Make sure that Outlook is open and your email with attachment will be sent")
    
    Exit Sub
err:
    MsgBox err.Description


End Sub
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,243
Members
448,555
Latest member
RobertJones1986

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