Create a .pdf and attach it to an email with a click of button in vb

Posted on 2004-11-04
Last Modified: 2010-05-02


I have report(s) created in Access that need to be converted to a .pdf and then need to be sent out as an attachment in an email .

I want to do this with a click of a button in a VBA.

Any suggestions or sample code would be much help.

The only thing I have so far is this...
'pdfwrite.PDFWrite (strreportname,path,link)

Basically I need under the button code to...

' Make PDF
 ' Email pdf
Question by:nomar2
    LVL 9

    Expert Comment

    by:Shahid Thaika
    You may need two sub-routines, one to to create the PDF file and another to mail it as an attachment.
    Please see this thread. It has links to things that might get you solving your problem.
    LVL 16

    Accepted Solution

    If you have an existing library to write to PDF (you mention pdfwrite.PDFWrite(...)), then this should be easy.  Otherwise, you could install CutePDF ( and print the document to this driver.

    Creating a mail is easy (I'm assuming that, with Access, you also have Outlook)

        Sub CreateMailWithAttachment(strTo as String, strSubject as String, strBody as String, _
                                                      strAttachPath As String, blnAutoSend as Boolean)

            Dim oOutlook, oMail

            Set oOutlook = CreateObject("Outlook.Application")
            Set oMail = oOutlook.CreateItem 0

            With oMail
               .Recipients.Add strTo
               .Subject = strSubject
               .Body = strBody
               .Attachments.Add strAttachPath
            End With

            If blnAutoSend = True Then oMail.Send Else oMail.Display

        End Sub

    Now call it, in your program, with

        CreateMailWithAttachment("", "Your report", _
                                               "This is your report", "x:\mypath\mypdffile.pdf", True)

    to send it automatically or

        CreateMailWithAttachment("", "Your report", _
                                               "This is your report", "x:\mypath\mypdffile.pdf", False)

    to display it on screen.




    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now