Printable Version of Topic

Click here to view this topic in its original format

UtterAccess Forums _ Access Q and A _ Pdf Attachment

Posted by: mike60smart Jul 16 2019, 11:17 AM

Hi Everyone

Can anyone tell me where I am going wrong with the following Code.

It Opens the Report for Preview and creates an EMail but does not attach the PDF Report to the EMail?

Any help appreciated

CODE
Private Sub cmdEmail_Click()
On Error Resume Next
If Me.Dirty = True Then Me.Dirty = False 'Saves any date not previously saved

    Dim OutApp As Object
    Dim OutMail As Object
    Dim strBody As String
    Dim strSubject As String
    Dim strTo As String
    Dim strDocname As String
    Dim strWhere As String
    Dim strEMailed As String
    
    strDocname = "rptOrderDetails"
    strWhere = strEMailed
    
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    strBody = "Hi," & vbNewLine & vbNewLine & _
    "Below please find latest Order details for your action." & vbNewLine & _
        "Thank you,"
    On Error Resume Next

    With OutMail
        .Display
        .To = Me.txtEMail
        .CC = ""
        .BCC = ""
        .Subject = "Order Details"
        .Where = strWhere
        .Body = strBody & vbNewLine & .Body
    End With
    
    DoCmd.OpenReport strDocname, acPreview, , strWhere
    DoCmd.SendObject acSendReport, "rptOrderDetails", acFormatPDF, strTo, , strSubject
  

    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing

  
End Sub


Posted by: theDBguy Jul 16 2019, 11:22 AM

Hi Mike. Looks like you're mixing two methods together. If you want to use Outlooks Automation, you don't need to use SendObject, but you'll need to use the OutputTo method. On the other hand, if you want to use SendObject, then you don't need to use Outlook Automation.

Posted by: DanielPineault Jul 16 2019, 11:25 AM

As theDBguy said, you need to pick which approach you wish to use and solely go with that technique.

Since you're not using any HTML tags... sendobject should be more than adequate and simpler. So I'd go with that and eliminate the Outlook automation code.

Probably something more like

CODE
Private Sub cmdEmail_Click()
    Dim strDocname            As String
    Dim strWhere              As String
    Dim strTo                 As String
    Dim strSubject            As String
    Dim strBody               As String

    On Error GoTo Error_Handler

    If Me.Dirty = True Then Me.Dirty = False    'Saves any date not previously saved

    strDocname = "rptOrderDetails"
    strWhere = strEMailed
    DoCmd.OpenReport strDocname, acPreview, , strWhere

    strTo = Me.txtEMail
    strSubject = "Order Details"
    strBody = "Hi," & vbNewLine & vbNewLine & _
              "Below please find latest Order details for your action." & vbNewLine & _
              "Thank you,"
    DoCmd.SendObject acSendReport, strDocname, acFormatPDF, strTo, , , strSubject, strBody

Error_Handler_Exit:
    On Error Resume Next
    Exit Sub

Error_Handler:
    MsgBox "The following error has occured" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: cmdEmail_Click" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occured!"
    Resume Error_Handler_Exit
End Sub



But I'm confused with the line
CODE
strWhere = strEMailed

where is strEMailed defined?

Posted by: mike60smart Jul 16 2019, 11:45 AM

Hi Everyone

I have simplified it to the following and now works ok.

Many thanks yet again
cheers.gif

CODE
Private Sub cmdEmail_Click()

    On Error GoTo cmdEmail_Click_Error
    
    
If Me.Dirty Then Me.Dirty = False
Dim strDocname As String
Dim strToWhom As String
Dim strMsgBody As String
Dim strSubject As String

strDocname = "rptOrderDetails"
strSubject = "Latest Order Details"
strToWhom = Nz(Me![EMail])
strMsgBody = "Find attached latest Customer Order Details."
If Me.Dirty Then Me.Dirty = False ' force a save

If strToWhom <> "" Then
DoCmd.OpenReport strDocname, acPreview
DoCmd.SendObject acSendReport, "rptOrderDetails", acFormatPDF, strToWhom, , , strSubject, strMsgBody, True
End If

    
  
    
    On Error GoTo 0
    Exit Sub

cmdEmail_Click_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdEmail_Click, line " & Erl & "."

End Sub

Posted by: theDBguy Jul 16 2019, 11:58 AM

Hi Mike. Glad to hear you got it sorted out. Cheers!