Save As pdf and email with outlook

lakeroof

New Member
Joined
Jan 30, 2013
Messages
15
The code I have below works as I want, except
It saves the whole workbook as pdf and I would like to save only "Sheet6"
It also does not insert my signature in outlook which I need
Any help to modify this would be appreciated.

VBA Code:
Sub Email_From_Excel_English()
    Dim emailApplication As Object
    Dim emailItem As Object
    Dim strPath As String
    Dim lngPos As Long
 
    ' Build the PDF file name
    strPath = ActiveWorkbook.FullName
    lngPos = InStrRev(strPath, ".")
    strPath = Left(strPath, lngPos) & "pdf"
    
    ' Export workbook as PDF
    ActiveWorkbook.ExportAsFixedFormat xlTypePDF, strPath
    Set emailApplication = CreateObject("Outlook.Application")
    Set emailItem = emailApplication.CreateItem(0)
    
    ' Now we build the email.
    emailItem.To = Range("C4")
    emailItem.Subject = "Roofing Estimate"
    emailItem.Body = "Please find attached your estimate, as well as a copy of our terms and conditions." & vbNewLine & vbNewLine & " If you have any questions, please do not hesitate to contact me." & vbNewLine & vbNewLine & "Regards," & vbNewLine & vbNewLine & signature
    
    ' Attach the PDF file
    emailItem.Attachments.Add strPath
    emailItem.Attachments.Add "C:\Users\Nick\Dropbox\1Nick\2022_Nick\Terms Conditions ENG.pdf"
    
    ' Send the Email
    ' Use this OR .Display, but not both together.
    emailItem.Display
    
    ' Display the Email so the user can change it as desired before sending it
    ' Use this OR .Send, but not both together.
    'emailItem.Display
    Set emailItem = Nothing
    Set emailApplication = Nothing
    ' Delete the PDF file
    Kill strPath
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Hi lakeroof,

how about

VBA Code:
Sub Email_From_Excel_English_mod()
' https://www.mrexcel.com/board/threads/save-as-pdf-and-email-with-outlook.1226032/
   Dim objAppOL As Object
   Dim objItemOL As Object
   Dim strPath As String
   Dim strBody As String

   ' Build the PDF file name
   strPath = ThisWorkbook.Path & "\Sheet6" & ".pdf"
   
   ' Export workbook as PDF
   Worksheets("Sheet6").ExportAsFixedFormat xlTypePDF, strPath
   Set objAppOL = CreateObject("Outlook.Application")
   Set objItemOL = objAppOL.CreateItem(0)
   
   ' Now we build the email.
   With objItemOL
    .Display
    strBody = .Body
    .To = Worksheets("Sheet6").Range("C4")
    .Subject = "Roofing Estimate"
    .Body = "Please find attached your estimate, as well as a copy of our terms and conditions." & _
        vbNewLine & vbNewLine & "If you have any questions, please do not hesitate to contact me." & _
        vbNewLine & vbNewLine & "Regards," & vbNewLine & vbNewLine & strBody
   
    ' Attach the PDF file
    .Attachments.Add strPath
    .Attachments.Add "C:\Users\Nick\Dropbox\1Nick\2022_Nick\Terms Conditions ENG.pdf"
    
    ' Send the Email
    ' Use this OR .Send, but not both together.
    .Display
   End With
   
   Set objItemOL = Nothing
   Set objAppOL = Nothing
   ' Delete the PDF file
   Kill strPath
End Sub

Ciao,
Holger
 
Upvote 0
Try this:

VBA Code:
Sub Email_From_Excel_English()
  Dim emailApplication As Object
  Dim emailItem As Object
  Dim strPath As String, sBody As String
  Dim lngPos As Long
  
  ' Build the PDF file name
  strPath = ActiveWorkbook.FullName
  lngPos = InStrRev(strPath, ".")
  strPath = Left(strPath, lngPos) & "pdf"
  
  ' Export workbook as PDF
  Sheets("Sheet6").ExportAsFixedFormat xlTypePDF, strPath
  Set emailApplication = CreateObject("Outlook.Application")
  Set emailItem = emailApplication.createitem(0)
  
  ' Now we build the email.
  emailItem.To = Range("C4").Value
  emailItem.Subject = "Roofing Estimate"
  
  ' Attach the PDF file
  emailItem.Attachments.Add strPath
  emailItem.Attachments.Add "C:\Users\Nick\Dropbox\1Nick\2022_Nick\Terms Conditions ENG.pdf"
  
  ' Send the Email
  ' Use this OR .Display, but not both together.
  emailItem.Display
  
  sBody = "Please find attached your estimate, as well as a copy of our terms and conditions." & _
    vbNewLine & vbNewLine & _
    " If you have any questions, please do not hesitate to contact me." & _
    vbNewLine & vbNewLine & "Regards," & vbNewLine & vbNewLine
  
  emailItem.HtmlBody = sBody & emailItem.HtmlBody
  
  ' Display the Email so the user can change it as desired before sending it
  ' Use this OR .Send, but not both together.
  'emailItem.Display
  Set emailItem = Nothing
  Set emailApplication = Nothing
  ' Delete the PDF file
  Kill strPath
End Sub
 
Upvote 0
On first I get run-time error "9"
' Export workbook as PDF
Worksheets("Sheet6").ExportAsFixedFormat xlTypePDF, strPath

Second one i get
Run time error 9
Subscript out of range at the same place
 
Upvote 0
It saves the whole workbook as pdf and I would like to save only "Sheet6"
Second one i get
Run time error 9
Subscript out of range at the same place
That's because you don't have a sheet in your workbook called "Sheet6".

Adjust your sheet name on this line and try again.
Sheets("Sheet6").ExportAsFixedFormat xlTypePDF, strPath

If you mean the codename then it should be like this:
VBA Code:
Sheet6.ExportAsFixedFormat xlTypePDF, strPath
 
Upvote 0
One more thing, if I may?
instead of
emailItem.Attachments.Add "C:\Users\Nick\Dropbox\1Nick\2022_Nick\Terms Conditions ENG.pdf"
Would it be possible to be able to choose a file (other users are using this file) ?
 
Upvote 0
Would it be possible to be able to choose a file
Try this:

VBA Code:
Sub Email_From_Excel_English()
  Dim emailApplication As Object
  Dim emailItem As Object
  Dim strPath As String, sBody As String, secondfile As String
  Dim lngPos As Long
  
  ' Build the PDF file name
  strPath = ActiveWorkbook.FullName
  lngPos = InStrRev(strPath, ".")
  strPath = Left(strPath, lngPos) & "pdf"
  
  ' Export workbook as PDF
  Sheet6.ExportAsFixedFormat xlTypePDF, strPath
  'Sheets("Sheet6").ExportAsFixedFormat xlTypePDF, strPath
  
  With Application.FileDialog(msoFileDialogFilePicker)
    .Title = "Select File"
    .AllowMultiSelect = False
    .InitialFileName = ThisWorkbook.Path & "\"
    If .Show Then
      secondfile = .SelectedItems.Item(1)
    End If
  End With

  
  Set emailApplication = CreateObject("Outlook.Application")
  Set emailItem = emailApplication.CreateItem(0)
  
  ' Now we build the email.
  emailItem.To = Range("C4").Value
  emailItem.Subject = "Roofing Estimate"
  
  ' Attach the PDF file
  emailItem.Attachments.Add strPath
  If secondfile <> "" Then
    emailItem.Attachments.Add secondfile
  End If
  
  ' Send the Email
  ' Use this OR .Display, but not both together.
  emailItem.display
  
  sBody = "Please find attached your estimate, as well as a copy of our terms and conditions." & _
    vbNewLine & vbNewLine & _
    " If you have any questions, please do not hesitate to contact me." & _
    vbNewLine & vbNewLine & "Regards," & vbNewLine & vbNewLine
  
  emailItem.HtmlBody = sBody & emailItem.HtmlBody
  
  ' Display the Email so the user can change it as desired before sending it
  ' Use this OR .Send, but not both together.
  'emailItem.Display
  Set emailItem = Nothing
  Set emailApplication = Nothing
  ' Delete the PDF file
  Kill strPath
End Sub
 
Upvote 0
With Application.FileDialog(msoFileDialogFilePicker)
"msoFileDialogFilePicker=empty"

Runtime error '-2147467259 (80004005)':
Method FileDialog of object'_Application'failed
 
Upvote 0
"msoFileDialogFilePicker=empty"
I don't understand that line. Did you add it to the code?
If you didn't modify the code, then maybe it's your version of excel or office.
Try this alternative:
VBA Code:
Sub Email_From_Excel_English()
  Dim emailApplication As Object
  Dim emailItem As Object
  Dim strPath As String, sBody As String, secondfile As Variant
  Dim lngPos As Long
  
  ' Build the PDF file name
  strPath = ActiveWorkbook.FullName
  lngPos = InStrRev(strPath, ".")
  strPath = Left(strPath, lngPos) & "pdf"
  
  ' Export workbook as PDF
  Sheet6.ExportAsFixedFormat xlTypePDF, strPath
  'Sheets("Sheet6").ExportAsFixedFormat xlTypePDF, strPath
  
  secondfile = Application.GetOpenFilename
  If secondfile = False Then
    secondfile = ""
  End If
  
'  With Application.FileDialog(msoFileDialogFilePicker)
'    .Title = "Select File"
'    .AllowMultiSelect = False
'    .InitialFileName = ThisWorkbook.Path & "\"
'    If .Show Then
'      secondfile = .SelectedItems.Item(1)
'    End If
'  End With

  
  Set emailApplication = CreateObject("Outlook.Application")
  Set emailItem = emailApplication.CreateItem(0)
  
  ' Now we build the email.
  emailItem.To = Range("C4").Value
  emailItem.Subject = "Roofing Estimate"
  
  ' Attach the PDF file
  emailItem.Attachments.Add strPath
  If secondfile <> "" Then
    emailItem.Attachments.Add secondfile
  End If
  
  ' Send the Email
  ' Use this OR .Display, but not both together.
  emailItem.display
  
  sBody = "Please find attached your estimate, as well as a copy of our terms and conditions." & _
    vbNewLine & vbNewLine & _
    " If you have any questions, please do not hesitate to contact me." & _
    vbNewLine & vbNewLine & "Regards," & vbNewLine & vbNewLine
  
  emailItem.HtmlBody = sBody & emailItem.HtmlBody
  
  ' Display the Email so the user can change it as desired before sending it
  ' Use this OR .Send, but not both together.
  'emailItem.Display
  Set emailItem = Nothing
  Set emailApplication = Nothing
  ' Delete the PDF file
  Kill strPath
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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