oEmail.Attachments.Add Help !

Sergio

Registered User.
Local time
Today, 11:13
Joined
Feb 28, 2002
Messages
42
Hello fellows:
In the code below, how can I attach an access report to mail it. Thank you in advance.

Private Sub btnSend_Click()
Dim oApp As New Outlook.Application
Dim oEmail As Outlook.MailItem

Set oEmail = oApp.CreateItem(olMailItem)

oEmail.To = Me.txtTo
oEmail.Subject = Me.txtSubject
oEmail.Body = Me.txtBody
oEmail.Attachments.Add
Me.txtAttachment.Value <-- I need to attach an access report. Not a file. But I don't know how?
With oEmail
If Not IsNull(.To) And Not IsNull(.Subject) And Not IsNull(.Body) Then
.Send
MsgBox "Email Sent!"
Else
MsgBox "Please fill out the required fields."
End If
End With

End Sub
 
you first convert the report to pdf using DoCmd.OutputTo.
then attached the created pdf.
Code:
Private Sub btnSend_Click()
    Dim oApp As New Outlook.Application
    Dim oEmail As Outlook.MailItem
    
    Const sReport As String = "yourReportNameHere"
    Dim sOutput As String
    
    sOutput = Environ("Temp") & "\" & sReport & ".pdf"
    'delete if already exists
    If Len(Dir(sOutput)) > 0 Then Kill sOutput
    'create the pdf
    DoCmd.OutputTo objecttype:=acOutputReport, _
                    objectname:=sReport, _
                    outputformat:=acFormatPDF, _
                    outputfile:=sOutput, _
                    outputquality:=acExportQualityPrint
    
    
    Set oEmail = oApp.CreateItem(olMailItem)
    
    oEmail.To = Me.txtTo
    oEmail.Subject = Me.txtSubject
    oEmail.Body = Me.txtBody
    oEmail.Attachments.Add sOutput  'Me.txtAttachment.Value <-- I need to attach an access report. Not a file. But I don't know how?
    With oEmail
        If Not IsNull(.To) And Not IsNull(.Subject) And Not IsNull(.Body) Then
            .Send
            MsgBox "Email Sent!"
        Else
            MsgBox "Please fill out the required fields."
        End If
    End With

End Sub
 
you first convert the report to pdf using DoCmd.OutputTo.
then attached the created pdf.
Code:
Private Sub btnSend_Click()
    Dim oApp As New Outlook.Application
    Dim oEmail As Outlook.MailItem
   
    Const sReport As String = "yourReportNameHere"
    Dim sOutput As String
   
    sOutput = Environ("Temp") & "\" & sReport & ".pdf"
    'delete if already exists
    If Len(Dir(sOutput)) > 0 Then Kill sOutput
    'create the pdf
    DoCmd.OutputTo objecttype:=acOutputReport, _
                    objectname:=sReport, _
                    outputformat:=acFormatPDF, _
                    outputfile:=sOutput, _
                    outputquality:=acExportQualityPrint
   
   
    Set oEmail = oApp.CreateItem(olMailItem)
   
    oEmail.To = Me.txtTo
    oEmail.Subject = Me.txtSubject
    oEmail.Body = Me.txtBody
    oEmail.Attachments.Add sOutput  'Me.txtAttachment.Value <-- I need to attach an access report. Not a file. But I don't know how?
    With oEmail
        If Not IsNull(.To) And Not IsNull(.Subject) And Not IsNull(.Body) Then
            .Send
            MsgBox "Email Sent!"
        Else
            MsgBox "Please fill out the required fields."
        End If
    End With

End Sub
Thanks a lot. I'll try it.
 
Hi. Pardon me, but I don't see anything in your code that will require the use of Outlook automation. If so, maybe you can just simply try using the SendObject method. It will also give you the advantage of not relying on Outlook alone. Just a thought...
 
I don't know why, but when I use SendObject method the mail sending is too slow and is not a discret method. I mean, Outlook App opens and then I have to wait to finish the files sending to close the pplication, because If I close before that, the files never leave from Outlook.
 
I don't know why, but when I use SendObject method the mail sending is too slow and is not a discret method. I mean, Outlook App opens and then I have to wait to finish the files sending to close the pplication, because If I close before that, the files never leave from Outlook.
Sorry, I can't say I've heard anyone have those issues before.
 
you first convert the report to pdf using DoCmd.OutputTo.
then attached the created pdf.
Code:
Private Sub btnSend_Click()
    Dim oApp As New Outlook.Application
    Dim oEmail As Outlook.MailItem
   
    Const sReport As String = "yourReportNameHere"
    Dim sOutput As String
   
    sOutput = Environ("Temp") & "\" & sReport & ".pdf"
    'delete if already exists
    If Len(Dir(sOutput)) > 0 Then Kill sOutput
    'create the pdf
    DoCmd.OutputTo objecttype:=acOutputReport, _
                    objectname:=sReport, _
                    outputformat:=acFormatPDF, _
                    outputfile:=sOutput, _
                    outputquality:=acExportQualityPrint
   
   
    Set oEmail = oApp.CreateItem(olMailItem)
   
    oEmail.To = Me.txtTo
    oEmail.Subject = Me.txtSubject
    oEmail.Body = Me.txtBody
    oEmail.Attachments.Add sOutput  'Me.txtAttachment.Value <-- I need to attach an access report. Not a file. But I don't know how?
    With oEmail
        If Not IsNull(.To) And Not IsNull(.Subject) And Not IsNull(.Body) Then
            .Send
            MsgBox "Email Sent!"
        Else
            MsgBox "Please fill out the required fields."
        End If
    End With

End Sub
I try your code and it works perfectly !!! I'm grateful to you. Please, let me know, what can I do for you. Cheers.
 

Users who are viewing this thread

Back
Top Bottom