Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PDF & Access 1

Status
Not open for further replies.

EEDirect

Technical User
May 19, 2005
18
Does anyone know how to take a report and send it via email as a PDF attachment so my customer an open the report in Adobe?

Thanks & Regards,
EEDirect
 
Yes. Please explain a bit more of how you see the process and I'll try to point you in the right direction.

Will you be printing the report to PDF from print preview and then want code to add the pdf as an attachment, or is it simply needing software to create a report in PDF format, or do you want the whole process done via code at the click of a button?
 
I would like the code to do it with a click of a button. My main goal is to have the information ready for email and then I will have the form convert to a PDF and automatically attach itself to another form which will be a kind-of cover letter to let the end user know what the attachment is, ie. a quote, or an invoice.
Thanks

Thanks & Regards,
EEDirect
 
There a are couple of ways to send items. One is a program from ACG which is very good called PDF and Mail
Another option is to print the report to a pdf file and then use some code to send out the email with the attachments. Below is a sample that I have used. The basis of this code came from elsewhere in Tek-Tips.

The code below will create the email and add an attachment using Microsoft Outlook.

This code should be placed in a module on the modules tab. You will need to create a reference to outlook. This code includes an error trap that attaches a pdf file called "Not Found". If you decide to include the not found attachment
you will need to create this file, which could be any file you want that has the name NotFound.pdf. Also, you will need to either save this file in C:\Files or change the code so that it points to the right location for notfound.pdf. Otherwise, you can comment out that section. You shouldn't need to change anything else on this module.


Code:
Function SendOutlookMessage(Recipients As String, Subject As String, Body As String, DisplayMsg As Boolean, Optional CopyRecipients As String, Optional BlindCopyRecipients As String, Optional Importance As Integer = 2, Optional AttachmentPath, Optional AttachmentOptionNumber As Integer)
'Function to create and send an outlook message with more control than sendobject
'separate multiple recipients or CC, or BCC with comma
'importance - 1=low, 2=normal, 3=high

'AttachmentOptionNumber allows additional Attachment based on which Option
'was clicked in order to send the email. - 05-Aug-2003

Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.recipient
Dim objOutlookAttach As Outlook.Attachment
Dim txtRecipient As String
Dim stAttachment As String 'Use to store original attachment so that order of attachments for ecp notice will be correct
Dim stattach As String
          ' Create the Outlook session.
          Set objOutlook = CreateObject("Outlook.Application")

          ' Create the message.
          Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

          With objOutlookMsg
                ' Add the To recipient(s) to the message.
                Do While InStr(1, Recipients, ",", vbTextCompare) <> 0 'checks for multiple recipients and adds each
                    txtRecipient = Left(Recipients, InStr(1, Recipients, ",", vbTextCompare) - 1)
                    Recipients = Trim(Mid(Recipients, Len(txtRecipient) + 2, Len(Recipients)))
                    Set objOutlookRecip = .Recipients.Add(txtRecipient)
                    objOutlookRecip.Type = olTo
                Loop
                
                Set objOutlookRecip = .Recipients.Add(Trim(Recipients))
                objOutlookRecip.Type = olTo

              ' Add the CC recipient(s) to the message if existing
            If CopyRecipients <> "" Then
                Set objOutlookRecip = .Recipients.Add(CopyRecipients)
                objOutlookRecip.Type = olCC
            End If
            
            ' Add the BCC recipient(s) to the message.
            If BlindCopyRecipients <> "" Then
                Set objOutlookRecip = .Recipients.Add(BlindCopyRecipients)
                objOutlookRecip.Type = olBCC
            End If
            
             ' Set the Subject, Body, and Importance of the message.
             .Subject = Subject
             .Body = Body & vbCrLf & vbCrLf
             Select Case Importance
                Case 1
                   .Importance = olImportanceLow
                Case 2
                    .Importance = olImportanceNormal
                Case 3
                    .Importance = olImportanceHigh
                Case Else
                    .Importance = olImportanceNormal
             End Select

             ' Add attachments to the message.
             On Error GoTo SendOutlookMessage_err
             If Not IsMissing(AttachmentPath) Then
                stAttachment = AttachmentPath
                
                If AttachmentOptionNumber = 1 Then
                    Set objOutlookAttach = .Attachments.Add(AttachmentPath)
                    On Error GoTo SendOutlookMessage_err
                    AttachmentPath = stAttachment
                End If
                
                Set objOutlookAttach = .Attachments.Add(AttachmentPath)
             End If

             ' Resolve each Recipient's name.
             For Each objOutlookRecip In .Recipients
                 objOutlookRecip.Resolve
             Next

             ' Should we display the message before sending?
             If DisplayMsg Then
                 .Display
             Else
                 .Save
                 .Send
             End If
          End With
          Set objOutlook = Nothing
          Exit Function
SendOutlookMessage_err:
      'If the file isn't found, attach the not found pdf file to
      'allow code to continue and user to manually attach file to
      'the email
      'Change path as appropriate or comment out this section if
      'not needing a generic not found attachment
      AttachmentPath = "C:\Files\NotFound.pdf"
      Resume
End Function

This code would be placed in a button on your form that will be clicked on to send the email with attachment. Modify the email ids and names as needed.

Code:
Private Sub SendEmail()
    'Send email with attachment

    '26 April 2005
    Dim stSubject As String
    Dim stTo As String
    Dim stCc As String
    Dim stBody As String
    Dim stDate As String
    Dim stPathFile As String
    Dim intOptionNumber As Integer    'Used for adding an additional attachment.  May need to be fixed.
    
'   Use person's name or email id separated by semicolons    
    stTo = "person name"
    stCc = "emailid1; emailid2"
    stSubject = "This is the subject for the email"
    stBody = "Hello, " & stTo & vbCrLf & vbCrLf & _
             "The attached is forwarded for your " & _
             "action.  Please respond via email by " & Date + 7 & "."
    stBody = stBody & _
             vbCrLf & vbCrLf & vbCrLf & _
             "Regards, " & vbCrLf & vbCrLf & _
             " "
             vbCrLf

    stPathFile = "C:\Temp\filename.pdf"
    intOptionNumber = 0
                    
    Call SendOutlookMessage(stTo, stSubject, stBody, True, stCc, , , stPathFile, intOptionNumber)
    
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top