Hello,
I am trying to piece together a macro that will convert a worksheet to PDF, save the PDF file name based off of cell data and attach it to a new email in Outlook.
I used Ron Debruin's code and combined it with Luke M's code (http://forum.chandoo.org/threads/vba-to-save-page-as-pdf-name-to-come-from-cell-values.11741/)
What I can't get to work is referencing the newly created PDF file to attach to the email.
When I run the code it creates the PDF and names it correctly - creates the new email in Outlook - but does not attach the newly created PDF.
I've attached the code below...
Help is much appreciated,
Jeff
I am trying to piece together a macro that will convert a worksheet to PDF, save the PDF file name based off of cell data and attach it to a new email in Outlook.
I used Ron Debruin's code and combined it with Luke M's code (http://forum.chandoo.org/threads/vba-to-save-page-as-pdf-name-to-come-from-cell-values.11741/)
What I can't get to work is referencing the newly created PDF file to attach to the email.
When I run the code it creates the PDF and names it correctly - creates the new email in Outlook - but does not attach the newly created PDF.
I've attached the code below...
Help is much appreciated,
Jeff
Code:
Sub RDB_Worksheet_Or_Worksheets_To_PDF_And_Create_Mail()
Dim FileName As String
If ActiveWindow.SelectedSheets.Count > 1 Then
MsgBox "There is more then one sheet selected," & vbNewLine & _
"be aware that every selected sheet will be published"
End If
'Call the function with the correct arguments
'Tip: You can also use Sheets("YourSheetName") instead of ActiveSheet in the code(sheet not have to be active then)
'Saves active worksheet as pdf using concatenation
'of G2,(" "),F3
Dim fName As String
With ActiveSheet
fName = .Range("G2").Value & (" ") & .Range("F3").Value
.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"C:\Users\Jeff\Desktop\" & fName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End With
'For a fixed file name use this in the FixedFilePathName argument
'FixedFilePathName:="C:\Users\Ron\Test\YourPdfFile.pdf"
If fName <> "" Then
RDB_Mail_PDF_Outlook FileNamePDF:="C:\Users\Jeff\Desktop\" & fName, _
StrTo:="person@email.com", _
StrCC:="", _
StrBCC:="", _
StrSubject:="This is the subject", _
Signature:=True, _
Send:=False, _
StrBody:="<H3><B>Dear Customer</B></H3><br>" & _
"<body>See the attached PDF file with the last figures."
Else
MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
"Microsoft Add-in is not installed" & vbNewLine & _
"You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
"The path to Save the file in arg 2 is not correct" & vbNewLine & _
"You didn't want to overwrite the existing PDF if it exist"
End If
End Sub
'Here are the functions that are called out....
Function RDB_Mail_PDF_Outlook(FileNamePDF As String, StrTo As String, _
StrCC As String, StrBCC As String, StrSubject As String, _
Signature As Boolean, Send As Boolean, StrBody As String)
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
If Signature = True Then .Display
.To = StrTo
.CC = StrCC
.BCC = StrBCC
.Subject = StrSubject
.HTMLBody = StrBody & "<br>" & .HTMLBody
.Attachments.Add FileNamePDF
If Send = True Then
.Send
Else
.Display
End If
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Function
Sub RDB_Worksheet_Or_Worksheets_To_PDF()
Dim FileName As String
If ActiveWindow.SelectedSheets.Count > 1 Then
MsgBox "There is more then one sheet selected," & vbNewLine & _
"be aware that every selected sheet will be published"
End If
'Call the function with the correct arguments
'Tip: You can also use Sheets("YourSheetName") instead of ActiveSheet in the code(sheet not have to be active then)
FileName = RDB_Create_PDF(Source:=ActiveSheet, _
FixedFilePathName:="", _
OverwriteIfFileExist:=True, _
OpenPDFAfterPublish:=True)
'For a fixed file name use this in the FixedFilePathName argument
'FixedFilePathName:="C:\Users\Ron\Test\YourPdfFile.pdf"
If FileName <> "" Then
'Ok, you find the PDF where you saved it
'You can call the mail macro here if you want
Else
MsgBox "Not possible to create the PDF, possible reasons:" & vbNewLine & _
"Microsoft Add-in is not installed" & vbNewLine & _
"You Canceled the GetSaveAsFilename dialog" & vbNewLine & _
"The path to Save the file in arg 2 is not correct" & vbNewLine & _
"You didn't want to overwrite the existing PDF if it exist"
End If
End Sub
[/CODE)]
Last edited: