Results 1 to 10 of 14

Thread: Add attachments to PDF output

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Add attachments to PDF output

    Ken,

    I have used your code to create (and name) a pdf, attach to an email and email all with a button click. But I need to know how to add 2-5 pdfs to the same email with the code I have written....or another code. I am using Excel 2003. Anyone have any thoughts? Here is my code:

    Code:
    Sub PrintToPDF_Paysheet()
    'Author : Ken Puls (www.excelguru.ca)
    'Macro Purpose: Print to PDF file using PDFCreator
    ' (Download from hxxp://sourceforge.net/projects/pdfcreator/)
    ' Designed for early bind, set reference to PDFCreator
    Dim pdfjob As PDFCreator.clsPDFCreator
    Dim sPDFName As String
    Dim sPDFPath As String
    Dim bRestart As Boolean
    Dim StrTo As String
    Dim StrSubject As String
    Dim StrBody As String
    Dim Send As Boolean
    Dim OutApp As Object
    Dim OutMail As Object
    '/// Change the output file name here! ///
    sPDFName = ActiveSheet.Range("C5").Text & ".pdf"
    sPDFPath = ActiveWorkbook.Path & Application.PathSeparator
    'Check if worksheet is empty and exit if so
    If IsEmpty(ActiveSheet.UsedRange) Then Exit Sub
    'Activate error handling and turn off screen updates
    On Error GoTo EarlyExit
    Application.ScreenUpdating = False
    Set pdfjob = New PDFCreator.clsPDFCreator
    'Check if PDFCreator is already running and attempt to kill the process if so
    Do
    bRestart = False
    Set pdfjob = New PDFCreator.clsPDFCreator
    If pdfjob.cStart("/NoProcessingAtStartup") = False Then
    'PDF Creator is already running. Kill the existing process
    Shell "taskkill /f /im PDFCreator.exe", vbHide
    DoEvents
    Set pdfjob = Nothing
    bRestart = True
    End If
    Loop Until bRestart = False
    'Assign settings for PDF job
    With pdfjob
    .cOption("UseAutosave") = 1
    .cOption("UseAutosaveDirectory") = 1
    .cOption("AutosaveDirectory") = sPDFPath
    .cOption("AutosaveFilename") = sPDFName
    .cOption("AutosaveFormat") = 0 ' 0 = PDF
    .cClearCache
    End With
    'Delete the PDF if it already exists
    If Dir(sPDFPath & sPDFName) = sPDFName Then Kill (sPDFPath & sPDFName)
    'Print the document to PDF
    ActiveSheet.PrintOut copies:=1, ActivePrinter:="PDFCreator"
    'Wait until the print job has entered the print queue
    Do Until pdfjob.cCountOfPrintjobs = 1
    DoEvents
    Loop
    pdfjob.cPrinterStop = False
    Application.Wait Now + TimeValue("0:00:03")
    'Wait until the file shows up before closing PDF Creator
    Do
    DoEvents
    Loop Until Dir(sPDFPath & sPDFName) = sPDFName
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    On Error Resume Next
    With OutMail
    If ActiveSheet.Range("C3").Value = "RE-ISSUE" Then
    .To = "Activesheet.Range("C1").value="HELP"
    ElseIf ActiveSheet.Range("C3").Value = "RE-ISSUE" & Space(1) & "AUTH" Then
    .To = ActiveSheet.Range("C4").Text
    
    End If
    .CC = ""
    .BCC = ""
    .Subject = ActiveSheet.Range("C5").Text & Space(1) & "RE-ISSUE PAYMENT"
    .Body = ""
    .Attachments.Add sPDFPath & sPDFName
    .Send 'or use .Display
    End With
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
    Cleanup:
    'Release objects and terminate PDFCreator
    Set pdfjob = Nothing
    Shell "taskkill /f /im PDFCreator.exe", vbHide
    On Error GoTo 0
    Application.ScreenUpdating = True
    Exit Sub
    EarlyExit:
    'Inform user of error, and go to cleanup section
    MsgBox "There was an error encountered. PDFCreator has" & vbCrLf & _
    "has been terminated. Please try again.", _
    vbCritical + vbOKOnly, "Error"
    Resume Cleanup
    End Sub
    Last edited by Ken Puls; 2013-07-04 at 04:33 PM. Reason: Added code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •