Question

Thanks in advance for any help. I have the following code which when clicked creates an email, inserts the body and customer email address and attaches a copy of the invoice. I have two questions, one is it possible to change the name of the attached file, at the moment it's just invoiceF.pdf could do with it being the reference of the delivery. AND secondly, is it possible to catch the result of the send mail? I know if the email isn't sent, just closed, access puts an information box up saying "The send object action was cancelled", i want to catch the "successfully sent" confirmation and add today's date to a box [invoiceemailed]. Thanks to anyone in advance of their help :)

Private Sub emailinvoiceF_Click()
On Error GoTo Err_emailinvoiceF_Click

If MsgBox("Email the invoice?", vbYesNo) = vbYes Then

Dim strMessage
strMessage = "Dear " & First & " " & Last & "," _
& vbCrLf & vbCrLf & "Thank you for your order: (" & DeliveryID & "), please find attached invoice." _
& vbCrLf & vbCrLf & "If you require any further information please do not hesitate to contact us." _
& vbCrLf & vbCrLf & "Kind Regards," _
& vbCrLf & vbCrLf & "SMI Hardwoods" _
& vbCrLf & vbCrLf & "Tel: 01206 396725" _
& vbCrLf & vbCrLf & "www.smi-hardwoods.com" _


    Dim stDocName As String

    stDocName = "InvoiceF"
    DoCmd.SendObject acReport, stDocName, acFormatPDF, [E-mail address], , , "SMI Hardwoods Invoice Ref:" & DeliveryID & ".pdf", strMessage
End If

Exit_emailinvoiceF_Click:
    Exit Sub

Err_emailinvoiceF_Click:
    MsgBox Err.Description
    Resume Exit_emailinvoiceF_Click

End Sub
Was it helpful?

Solution

DoCmd.SendObject command is very limited. It does not allow you attaching files from disk. Because of this limitation you have no control over the file name and number of files to be attached using DoCmd.SendObject.

You may find this article helpful: http://msdn.microsoft.com/en-us/library/aa167323(v=office.11).aspx.

I used Outlook.Application object in the past and found it OK. It allows to have some user interaction before sending an email. Also, it goes straight into Outlook so tracking might be easier. This approach has its own cons, though.

As per your question regarding the Success message, you can add the following to your code:

....
    DoCmd.SendObject acReport, stDocName, acFormatPDF, [E-mail address], , , "SMI Hardwoods Invoice Ref:" & DeliveryID & ".pdf", strMessage

    'Display success message
    MsgBox("Successfully sent on: " & Date())
End If

Exit_emailinvoiceF_Click:
    Exit Sub
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top