Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Macro - Send Active Sheets as PDF

0

Hi,

I have have just started using the macro to send the Active Workbook as an email (link below from YouTube)

https://www.youtube.com/watch?v=bxsvO6_uJUs

Is it possible to adapt this so that it sends the Active Sheets as PDF instead of of xlsx workbook?

Many Thanks

Answer
Discuss

Answers

0
Selected Answer

Hi Boytjie79

If the tutorial emails an entire workbook, I guess it is based on the ActiveWorkbook.ExportAsFixedFormat method (which is fine for workbooks).

If you use Outlook, the recognised expert on emailling (and other things!) is Ron de Bruin. On his webpage Ron de Bruin Excel Automation you'll find a downloadable zip file containing several macros. One can create a pdf from active sheets but I suggest you try "Macro 2 : Create and mail a PDF of the ActiveSheet or selected sheets".

Hope this helps. (You're new to the Forum, so please remember to Select whichever Answer best resolves your problem/ meets your needs)

Discuss
0

You can run the below VBA code to automatically save the active worksheet as a PDF file, and then email it as an attachment through Outlook. Please do as follows.

1. Open the worksheet you will save as PDF and send, then press the Alt + F11 keys simultaneously to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, click Insert > Module. Then copy and paste the below VBA code into the Code window.

VBA code: Save a worksheet as a PDF file and email it as an attachment

Sub Saveaspdfandsend()
Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range

Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)

If xFileDlg.Show = True Then
   xFolder = xFileDlg.SelectedItems(1)
Else
   MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
   Exit Sub
End If
xFolder = xFolder + "\" + xSht.Name + ".pdf"

'Check if the file already exists
If Len(Dir(xFolder)) > 0 Then
    xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
                      vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbYes Then
        Kill xFolder
    Else
        MsgBox "if you don't overwrite the existing PDF, I can't continue." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
        Exit Sub
    End If
    If Err.Number <> 0 Then
        MsgBox "Unable to delete existing file.  Please make sure the file is not open or write protected." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
        Exit Sub
    End If
End If

Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
    'Save as PDF file
    xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard

    'Create Outlook email
    Set xOutlookObj = CreateObject("Outlook.Application")
    Set xEmailObj = xOutlookObj.CreateItem(0)
    With xEmailObj
        .Display
        .To = ""
        .CC = ""
        .Subject = xSht.Name + ".pdf"
        .Attachments.Add xFolder
        If DisplayEmail = False Then
            '.Send
        End If
    End With
Else
  MsgBox "The active worksheet cannot be blank"
  Exit Sub
End If
End Sub

Let me know if this works for you.

Regards,
Jerry M.

Discuss

Discussion

Hi Jerry,
Thanks for the code. I have found a means to send in the body of the email but will give this a go to see if it works for the PDF version.
Appreciate you help.
Cheers
Boytjie79 (rep: 4) Dec 22, '20 at 9:49 am
@ Jerry- good work! I found that works very well (though I was a bit confused about the file requestor wanting just a folder name). I tried the .ExportAsFixedFormat method but it didn't work at sheet level (don't know what I did wrong!)

@Boytjie79 - I recommend you try Jerry's code- it has the advantage that the pdf includes both images and conditional formatting. 
John_Ru (rep: 6142) Dec 23, '20 at 3:11 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login