Forum Discussion
Select range in Excel to print to Pdf and send by Outlook
Hello,
I´m really noobie in Excel Vba but i really want a Macro that allows me to select a range in Excel, print that selection to Pdf and send it by Outlook.
I managed to achieve most of it (modified some code found in websites) but i´m missing the following and since i´m not a programmer i stuck....
I´m still missing this:
Important to me
- Choose the range in the sheet to print to Pdf (it can be on the Vba code, but i would prefer a sheet cell reference)
- Choose the name of the sheet to print, because this macro can be useful for other sheets i have ((it can be on the Vba code, but i would prefer a sheet cell reference)
Not so important:
- Choose the default computer path to save the document, for example "c:\pdf_prints\"  . I would prefer to write the path in a excel cell, it´s more flexible to use in another sheets
- Is there a way of deleting the pdf file? Just send it in the email.
I sent the file in attachement. Can someone help please?
Code below:
- Sub Save_as_pdf_and_send()
- 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 ActiveSheet.Range("n9") & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
- Exit Sub
- End If
- 'xFolder = ActiveSheet.Range("n9") 'Not working properly
- xFolder = xFolder + "\" + ActiveSheet.Range("n10") + ".pdf" 'xSht.Name + ".pdf"_Can´t_have_bars
- 'Check if file already exist
- 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 = ActiveSheet.Range("n5")
- .CC = ActiveSheet.Range("n6")
- .Subject = ActiveSheet.Range("n7")
- .HTMLBody = "<font face=" & Chr(34) & "Calibri" & Chr(34) & " size=" & Chr(34) & 4 & Chr(34) & ">" & "Good day dear Master," & "<br> <br>" & ActiveSheet.Range("n8") & "<br> <br>" & signature & "</font>"
- .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