Define PDF file and attache to mail


New Member
Jan 11, 2013
Hello All --

In a user form I define a PDF file and it is created successfully.

In a seperate module, I define another PDF file and it too is created successfully and attahced to an outlook mail.

I want both PDF's to attach to the mail but have been unsuccessful in doing so thus far.

Code is below. If fails at add.attachment......PDFfile2. Pdffile 1 does attach successfully

Any thoughts are appreciatted. Code Bleow

Thanks, John

User Form
Rich (BB code):
Private Sub cbARCOS2_Click()
Sheets(Array("ARCOS 2")).Select
End Sub
Private Sub CommandButton1_Click()
  ' Define PDF filename
  PdfFile2 = Environ("temp") & "\" & ActiveWorkbook.FullName
  i = InStrRev(PdfFile2, ".")
  If i > 1 Then PdfFile2 = Left(PdfFile2, i - 1)
  PdfFile2 = Sheets("Service Report").Range("H12") & " - " & Sheets("Service Report").Range("C9") & " - " & Sheets("Service Report").Range("H11") & " - " & Sheets("Service Report").Range("G4") & " - " & "PM Checklist" & ".pdf"
  ' Export sheet(s) as PDF
     With ThisWorkbook.ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=Environ("temp") & "\" & PdfFile2, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
    End With
Unload Me
End Sub

And then in the module (pdf one is created withing this module earlier in the code but I did post all that)

Rich (BB code):
  ' Use already open Outlook if possible
  On Error Resume Next
  Set OutlApp = GetObject("Outlook.Application")
  If Err Then
    Set OutlApp = CreateObject("Outlook.Application")
    IsCreated = True
  End If
  OutlApp.Visible = True
  OutMail.Visible = True
  On Error GoTo 0
 ' Prepare e-mail with PDF attachment
  With OutlApp.CreateItem(0)
    ' Prepare e-mail
    .Subject = Title
    .To = Sheets("Service Report").Range("C14").Value '<-- Put email of the recipient here
    .cc = Sheets("Service Report").Range("C15").Value & ";" & " " & "" ' <-- Put email of 'copy to' recipient here
    .HTMLBody = "Hello" & " " & Sheets("Service Report").Range("C12").Value & "," & "<br><br>" & _
           "Thank you for choosing SPECTRO.  Your service report is attached. Save the filled form attachment for your own records." & "<br><br>" & _
           "Regards," & " " & "<br><br>" & _
           Application.UserName & "<br><br>" & _
           "<a href=''> How are we doing?  Survey takes less than 3 minutes. </a>"
    .Attachments.Add Environ("temp") & "" & PdfFile1
    .Attachments.Add Environ("temp") & "" & PdfFile2

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...