Emailing as PDF instead of xlsx

rlink_23

Board Regular
Joined
Oct 30, 2015
Messages
149
so I have a sheet that I have set up to email the selected sheet when you click email. It finds the email address in "G14" and sends the email as an xlsx. I have decided I think a PDF will be a b etter format to send so that way the customer doesn't have the option to edit the sheet... I have tried Ron De Bruin's code to no avail... The current code I have looks like this

Code:
Sub Mail_Every_Worksheet()

    Dim wb As Workbook
    Dim FileExtStr As String
    Dim FileFormatNum As Long
    Dim TempFilePath As String
    Dim TempFileName As String
    Dim OutApp As Object
    Dim OutMail As Object


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


   
        FileExtStr = ".xlsm": FileFormatNum = 52
    End If


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


    Set OutApp = CreateObject("Outlook.Application")


    For Each sh In ThisWorkbook.Worksheets
        If sh.Range("G14").Value Like "?*@?*.?*" Then


            sh.Copy
            Set wb = ActiveWorkbook


            TempFileName = "Sheet " & sh.Name & " of " _
                         & ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss")


            Set OutMail = OutApp.CreateItem(0)


            With wb
                .SaveAs TempFilePath & TempFileName & FileExtStr, FileFormat:=FileFormatNum


                On Error Resume Next
                With OutMail
                    .To = sh.Range("G14").Value
                    .CC = ""
                    .BCC = ""
                    .Subject = "Engine Quote"
                    .Body = "Here is the engine quote you requested. Please call with any questions. Thank you!"
                    .Attachments.Add wb.FullName
                   
                    .Send   
                End With
                On Error GoTo 0


                .Close savechanges:=False
            End With
            
            Set OutMail = Nothing


            Kill TempFilePath & TempFileName & FileExtStr


        End If
    Next sh


    Set OutApp = Nothing


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


End Sub

Now can I use this same code just change a little bit to send as pdf? Or am I opening a whole new batch of problems? Please help???
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Change the For... Next loop to:
Code:
    For Each sh In ThisWorkbook.Worksheets
        If sh.Range("G14").Value Like "?*@?*.?*" Then

            TempFileName = TempFilePath & "Sheet " & sh.Name & " of " _
                         & ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss") & ".pdf"
            
            sh.ExportAsFixedFormat Type:=xlTypePDF, Filename:=TempFileName, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

            Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next
            With OutMail
                .To = sh.Range("G14").Value
                .CC = ""
                .BCC = ""
                .Subject = "Engine Quote"
                .Body = "Here is the engine quote you requested. Please call with any questions. Thank you!"
                .Attachments.Add TempFileName
               
                .Send
            End With
            On Error GoTo 0
            
            Set OutMail = Nothing

            Kill TempFileName

        End If
    Next sh
 
Upvote 0
Joh that worked great... The only question I have is when I test the code, and sent an email to myself. It shows 1 of 1293 Sheet... I have only the one sheet that needs emailed... Is there a way to change that so it is only one sheet???? If somebody goes to print the PDF it will print all the sheets unless they know to actually only print page one! Thanks again for your help by the way!!! :)
 
Upvote 0
If it shows as 1 of 1293 sheets....you probably have data somewhere else on the sheet.
on the sheet in question Press CTRL + END and see where the cursor ends up.
If it's way down the sheet somewhere, you will need to delete all rows / columns outside the used range
 
Upvote 0
To email just the active sheet, replace my previous code with:
Code:
    Dim emailAddress As String

    With ThisWorkbook.ActiveSheet
        If .Range("G14").Value Like "?*@?*.?*" Then
            emailAddress = .Range("G14").Value

            TempFileName = TempFilePath & .Name & " " & ThisWorkbook.Name & " " & Format(Now, "dd-mmm-yy h-mm-ss") & ".pdf"
            
            .ExportAsFixedFormat Type:=xlTypePDF, Filename:=TempFileName, _
                Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

            Set OutMail = OutApp.CreateItem(0)

            On Error Resume Next
            With OutMail
                .To = emailAddress
                .CC = ""
                .BCC = ""
                .Subject = "Engine Quote"
                .Body = "Here is the engine quote you requested. Please call with any questions. Thank you!"
                .Attachments.Add TempFileName
               
                .Send
            End With
            On Error GoTo 0
            
            Set OutMail = Nothing

            Kill TempFileName

        End If
    End With
 
Upvote 0
not to open an old thread but too late...awesome piece of code here!

Just to clarify and helpful to me:

G14 accepts any email address. I plan to tweak this to pull the email address from another sheet in my workbook (hidden sheet).
I also would like to tell it to send to all all email addresses, say G14, G15, etc to G23 (up to 10 recipients)- don't know if i'll need 10 but gives me future expansion room.

Any reason I can't tweak to set lines like .Subject = subject and subject = .Range("A1") ? Then it's easy and quick for a user to tweak.

Thanks for the input. I plan to attach this in a module with a button on the activesheet so it can be easily sent on demand! Thanks!

Also- running excel adn have outlook installed on machine. This should send through outlook without actually opening, correct?

Thanks again
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,996
Members
452,373
Latest member
TimReeks

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