Moving away from DoCmd.SendObject

crann

Registered User.
Local time
Today, 04:17
Joined
Nov 23, 2002
Messages
160
Hi

I currently send a copy of an Invoice report using the following code that all works fine:

Private Sub Command81_Click()

Dim stDocName As String

stDocName = "ReportInvoice"

DoCmd.OpenReport stDocName, acPreview, , "[ID] = " & Me.ID

DoCmd.SendObject acSendReport, "RptInvoice", acFormatPDF, "emailaddress",, , "***Copy of Invoice***", "Copy of invoice sent, attached"

End Sub




The problem is I now want to request a Receipt when read and send the email in silence.

I believe I am unable to do this using Docmd.SendObject so have spent some time looking at other code which utilises code such as:

olitem.ReadReceiptRequested = True
and
olitem.Send

the problem is I do not know what code to use to attach the specific invoice report that I am actually trying to email.

I have found examples of code that will allow me to attach a file from a fixed location example C:\myfolder.pdf

Can anyone help me with code to email the specific report i want to send and send it silent with a read receipt.

Thank you
 
The simplest route is to save your report locally as a pdf then attach it using your outlook object;

Code:
Set OutApp = GetObject(, "Outlook.Application")
    If OutApp Is Null Then
        Set OutApp = CreateObject("Outlook.Application")
    End If
    
    OutApp.Session.Logon
    Set OutMail = OutApp.CreateItem(0)
   
    With OutMail
        .Display
    End With
        signature = OutMail.body
        
    With OutMail
    
        .To = Variable_To
        .CC = ""
        .BCC = ""
        .Subject = Variable_Subject
        .MailItem.ReplyRecipients.Add = "your_reply_address_here@dot.com"
        .SentOnBehalfOfName = "accounts@your_domain.com"
        .Attachments.Add (Variable_AttachmentFile)
        .Display   'or use .Send to send immediatey
        .ReadReceiptRequested = False  ' or True
    End With
 
try:
usage:

vSuccess = Email1("rick.grimes@TWD.com","subject", "body text",true, "c:\folder\file.zip",)


Code:
Public Function Email1(ByVal pvTo, ByVal pvSubj, ByVal pvBody, byval pbReceipt as boolean, optional ByVal pvFile) As Boolean
Dim oApp As Outlook.Application
Dim oMail As Outlook.MailItem

On Error GoTo ErrMail

    'NOTE  BE SURE YOU ADD OUTLOOK APP VIA  VBE menu:TOOLS, REFERENCES

Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(olMailItem)

With oMail
    .To = pvTo
    .Subject = pvSubj
    .Body = pvBody
    if pbReceipt then .ReplyRecipients.Add = "your_reply_address_here@dot.com"

    if not isempty(pvFile) then  .Attachments.Add pvFile, olByValue, 1
    .Send
End With

Email1 = True
endIt:
Set oMail = Nothing
Set oApp = Nothing
Exit Function

ErrMail:
MsgBox Err.Description, vbCritical, Err
Resume endit
End Function
 
Thanks

We issue a lot of invoices daily so just want to send copy of the invoice directly from the Invoice form, we also share our Tables through sharepoint to allow us to access our database from various sites. I prefer my reports to be stored within the database and then just email whichever Invoice we are looking at, if that makes sense.

Obviously I would need some way of usually the specific Invoices ID would I not?
 
Create the invoice using your current report method, just save temporarily as a local pdf instead of sending it. Then attach it to the email, send it, then delete the local tmp copy.

sLocalQuotePath = Application.CurrentProject.Path & "\TmpInvoices"

DoCmd.OpenReport stDocName, acPreview, , "[ID] = " & Me.ID
DoCmd.OutputTo acOutputReport, acFormatPDF, sLocalQuotePath & "\Invoice_Copy_" & me.ID & ".pdf", False, , , acExportQualityPrint

Variable_AttachmentFile = sLocalQuotePath & "\Invoice_Copy_" & me.ID & ".pdf"
 
Last edited:
Thanks

Sorry my knowledge isn't great with code as Im still learning, so im completely lost on what code to here.

For my own interest could you explain why we have to create a temp copy, is it not possible to create a piece of code that emails whichever record I have open within the form but simply sends it without displaying outlook and then requests a read receipt.

:banghead:

thanks so much
 
and where should i be placing all this could on the Cmd button?

thanks
 
There are other ways to email from access but they would nearly all require more complex coding than using the outlook object. They often also require additional libraries installing, which in a multi-user, multi versions of office can be an issue.
I think a read receipt is very awkward to request without using outlook.

The code should be on a cmd button.

The reason for creating the temporary file is to enable you to use the outlook object that lets you request a read receipt and send the email.

As far as I'm aware you can't do the outlook specific stuff without using the outlook object, and therefore to add the attachment I think you have to create the temporary file.

Therefore in your code attached to the command button the major steps are;

1. Create and store the temp pdf - you already are almost doing this
2. Create the email , attach the file, send the email.
3. Delete the temp copy

There are some items that might trip you up on your journey, you would be wise to test that the folder you choose to store the file in exists and if not create it, before trying to save it.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom