Link to home
Start Free TrialLog in
Avatar of Chris_Sizer
Chris_Sizer

asked on

HTML Email with PDF Attachment

Hi,

I've already got VBA code that using an exisitng MS Access report and puts it into the body of the email.

What i'm now looking to do is have another report attached to this email as a PDF?  Is this at all possible?

The current code I have (included below) generates a seperate email with the PDF attachement but I want to be able to add the PDF to the 1 email with the HTML report details?

Any assistance you can provide would be greatly appreciated.

Thx

Dim objOutlook          As Outlook.Application
Dim objOutlookMsg       As Outlook.MailItem
Dim objOutlookRecip     As Outlook.Recipient
Dim strReportName       As String
Dim strReportPath       As String
Dim strReportFullPath   As String
Dim stDocName           As String
Dim strMyString         As String
Dim strReportText       As String
Dim strTo               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. Substitute
        ' your names here.
        strTo = "TO details"
        Set objOutlookRecip = .Recipients.Add(strTo)
        objOutlookRecip.Type = olTo
        
        ' Add the CC recipient(s) to the message.
        strCC = "CC details"
        Set objOutlookRecip = .Recipients.Add(strCC)
        objOutlookRecip.Type = olCC
        
        ' Add the Subject to the message.
        strSubject = "Subject Text"
        .Subject = (strSubject)
                
        'Set the variables
        strReportPath = "Path to save report"
        strReportName = "Report Name"
        strReportFullPath = strReportPath & strReportName
            
            'Open the report, hidden an filtered to the current form record
            DoCmd.OpenReport strReportName, acViewPreview, , "id=" & Me.id, acHidden
            'Output the report as HTML
            DoCmd.OutputTo acOutputReport, strReportName, acFormatHTML, strReportFullPath & ".html"
            'Close the Report
            DoCmd.Close acReport, strReportName
        
            '--- Create a single line text file from the HTML Report,
            '--- in order to be displayed propely as an HTML Document in the Email body
            '--- Code modified from here:
            '--- http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_21294504.html#a13177554
            'Open file for input (modification)
            Open strReportFullPath & ".HTML" For Input As #1
            Do While Not EOF(1)
                'Read each Line into strMyString variable.
                Line Input #1, strMyString
                strReportText = strReportText & strMyString     'Put all the Lines read into one String, FullText
            Loop
            Close #1
            
        'Insert the Full Report (as a simgle Line string) into the email body
        .HTMLBody = strReportText
        
        ' Add the attachment to the message
        stDocName = "Store Logins"
        DoCmd.SendObject acReport, stDocName, acFormatPDF

        ' Resolve each Recipient's name.
        For Each objOutlookRecip In .Recipients
            If Not objOutlookRecip.Resolve Then
                objOutlookMsg.Display
            End If
        Next
        
        'Send email without viewing it.
        '.Save
        '.Send
        
        'Dispay email before sending.
        .Display
        
    End With

'Cleanup Code
Set objOutlookMsg = Nothing
Set objOutlook = Nothing
Set objOutlookRecip = Nothing
End Sub

Open in new window

Avatar of mbizup
mbizup
Flag of Kazakhstan image

You can add a attachment with this line anywhere in the With block for your Message object:

 .Attachments.Add("C:\YourFolder\YourReport.PDF")

Open in new window

If you want to use a variable for your attachment:

Dim strReportText       As String
Dim strTo               As String
Dim strAttach as string   '<----*** Add this


strAttach = "C:\YourFolder\YourReport.PDF"   '<----*** Add this

    
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    With objOutlookMsg
           .Attachments.Add strAttach   '<----*** Add this

'  Etc....

Open in new window

Avatar of Chris_Sizer
Chris_Sizer

ASKER

Thanks for your reply mbizup!

My problem with this is that i need to generate a specific PDF for the particular email i'm sending which generates from another report in the database.

Is it then possible to have this PDF attached to the email instead of using a stored PDF?

Thx
ASKER CERTIFIED SOLUTION
Avatar of mbizup
mbizup
Flag of Kazakhstan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial