简体   繁体   中英

Access VBA code: Save and Email output individually using outlook

Been trying build a VBA code that automatically save and send the output to each Email_ID. Just started learning vba code in access.

I'm facing an error in DoCmd.SendObject part, "Item not found in this collection".

I would really appreciate if anyone can help me solve this error.

Private Sub Command_PDF_Click()

    Dim myrs As Recordset
    Dim myPDF, myStmt As String
    Dim oApp As New Outlook.Application, oEmail As Outlook.MailItem

    myStmt = "Select distinct Code1 from Query_Certificate_Eng"
    Set myrs = CurrentDb.OpenRecordset(myStmt)

    Do Until myrs.EOF

        myPDF = "C:\Users\93167\Desktop\Output Certificate\" & Format(myrs.Fields("Code1"), "0000000000000") & ".pdf"

        DoCmd.OpenReport "Certificate_Eng", acViewPreview, , "Code1 = '" & myrs!Code1 & "'"

        DoCmd.OutputTo objectType:=acOutputReport, objectName:="Certificate_Eng", outputformat:=acFormatPDF, outputfile:=myPDF, outputquality:=acExportQualityPrint
        
        DoCmd.SendObject objectType:=acSendReport, objectName:="Certificate_Eng", outputformat:=acFormatPDF, To:=myrs.Fields("Email_ID"), Subject:="Test", MessageText:="Test Message"
        
               
        DoCmd.Close
        myrs.MoveNext

    Loop

myrs.Close
Set myrs = Nothing
End Sub
myStmt = "Select distinct Code1 from Query_Certificate_Eng"
Set myrs = CurrentDb.OpenRecordset(myStmt)
...
..., To:=myrs.Fields("Email_ID")

This cannot work. Your recordset only contains the field Code1 , so Email_ID will be "not found in this collection".

You must have it in the SELECT clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM