Results 1 to 11 of 11
  1. #1
    scallebe is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Location
    Bornem (Antwerp - Belgium)
    Posts
    13

    how to send generated pdf file as attachment in email

    Hello specialists,

    I have a code that printout my report to the printer (2 copies) and create that same report as a pdf file to a folder with the combination from 2 fields out of the form as title.



    Code:
    Private Sub PrintDoc_Click()
    Dim folder As String
    Dim strDocName As String
    Dim strWhere As String
    strDocName = "Verschilstaat"
    folder = CurrentProject.Path & "\Verschilstaten - Decomptes  - PDF\"
    DoCmd.PrintOut acPrintAll, , , , 2
    On Error GoTo Opslaan_cmdReportOpslaan
        MkDir folder
        Resume Opslaan_cmdReportOpslaan
    Opslaan_cmdReportOpslaan:
        DoCmd.OutputTo acOutputReport, strDocName, acFormatPDF, folder & [Forms]![FormMilitairen]![national_number] _
        & " - Verschilstaat Nr " & [Forms]![FormMilitairen]![SubFormVerschilstaat].[Form]![VerschilstaatID] & ".pdf"
    DoCmd.Close acReport, "Verschilstaat", acSaveNo
    End Sub
    It looks a little like this :

    65100128355 - Verschilstaat Nr 779.pdf

    So far no problem...

    Now I like to add that same generated PDF as an attachment to a Email and let the user decide to send the mail or not...

    I looked around on the forum and the Web but could't find a solution for my specific problem.

    Is there somebody who could help me with this?

    Thanks

    Pascal

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    this code is everywhere on the forum. Dont know how you missed it.

    Code:
        sFile = folder & [Forms]![FormMilitairen]![national_number] & " - Verschilstaat Nr " & [Forms]![FormMilitairen]![SubFormVerschilstaat].[Form]![VerschilstaatID] & ".pdf"
        DoCmd.OutputTo acOutputReport, strDocName, acFormatPDF, sFile
        sTo = "bob@acme.com"
        sSubj = "your data"
        sBody = "dear bob, here's your data"
    
        call Email1(sTo, sSubj, sBody, sFile)

    put this code into a module for all to use.
    Code:
    Public Function Email1(ByVal pvTo, ByVal pvSubj, ByVal pvBody, Optional ByVal pvFile) As Boolean
    Dim oApp As Outlook.Application
    Dim oMail As Outlook.MailItem
    On Error GoTo ErrMail
    
    'NOTE : YOU MUST HAVE THE OUTLOOK REFERENCE CHECKED IN VBE; ctl-G, menu,tools, references, Microsoft Outlook XX Object library
    
    Set oApp = CreateObject("Outlook.Application")
    Set oMail = oApp.createitem(olMailItem)
    With oMail
        .To = pvTo
        .Subject = pvSubj
        If Not IsNull(pvBody) Then .Body = pvBody
        If Not IsMissing(pvFile) Then .Attachments.Add pvFile, olByValue, 1
        
        .Display True
        
        '.Save    'draft, we are NOT sending...we save as draft
        .Send     
        
    End With
    Email1 = True
    endit:
    Set oMail = Nothing
    Set oApp = Nothing
    Exit Function
    ErrMail:
    MsgBox Err.Description, vbCritical, Err
    Resume endit
    End Function

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Do you really need to save report as PDF?

    SendObject can accomplish without first saving to external file. However, cannot dictate name of PDF that is attached to email.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    scallebe is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Location
    Bornem (Antwerp - Belgium)
    Posts
    13
    Thanks Ranman for your reply,

    With a few adjustments f.e. the body, it works perfect...

    One more thing : How can I add my default Signature to the mail?

    Now I have a mail without my default signature.

    Thanks

    Pascal

  5. #5
    scallebe is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Location
    Bornem (Antwerp - Belgium)
    Posts
    13
    June7,

    Thanks for your reply.

    Our internal rules demand that we have a copy for each document (in PDF) we send to our clients. We have to keep them for 5 years...

    This way we don't need to have a physical file with documents who has "medical secret"...

    Greetz

    Pascal

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Here is one discussion on email signature https://www.accessforums.net/showthread.php?t=71867. Search forum for more.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    scallebe is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Location
    Bornem (Antwerp - Belgium)
    Posts
    13
    June7,

    Thanks for your reply...

    I saw that post already, but I wasn't able to add it in the code from ranman256 #2. His solution for my main question works perfect and I don't want to mess it up.

    I tried to add some lines in the code, but it doesn't work...

    Greetz

    Pascal

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Will have to modify code within the loop. Referenced link provides 2 methods I know work.

    Your signature includes image?

    Provide your attempted code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    scallebe is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Location
    Bornem (Antwerp - Belgium)
    Posts
    13
    June7

    Thanks for your reply,

    Yes it does have a image, the rest is text

    Click image for larger version. 

Name:	Knipsel.JPG 
Views:	21 
Size:	33.2 KB 
ID:	37635

    Thanks

    Greetz

    Pascal

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    So modify code as suggested and if it still has issue, post it for analysis.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  11. #11
    scallebe is offline Novice
    Windows 10 Access 2016
    Join Date
    Mar 2018
    Location
    Bornem (Antwerp - Belgium)
    Posts
    13
    Jene7,

    Thanks for your reply.

    If I understand you correctly, I have to modify the code provided by Ranman256 with the suggestions in the link of #6

    That will be a challenge for me...

    I will try it, but it will be next tuesday when I'm back at the office, I will keep you posted…

    Greetz have a nice weekend

    Pascal

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Send email with attachment
    By kiwikiki718 in forum Access
    Replies: 3
    Last Post: 04-09-2017, 08:30 PM
  2. Send attachment as email attachment.
    By JulieAHop in forum Programming
    Replies: 1
    Last Post: 03-10-2017, 10:25 AM
  3. Send email with PDF attachment
    By Rzadziu in forum Reports
    Replies: 8
    Last Post: 10-14-2016, 07:08 AM
  4. Send email with Attachment
    By scoe in forum Programming
    Replies: 3
    Last Post: 09-23-2014, 03:02 AM
  5. How to send email without attachment
    By behnam in forum Programming
    Replies: 3
    Last Post: 07-30-2014, 08:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums