UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Reply to this topicStart new topic
> Pdf Attachment, Access 2016    
 
   
mike60smart
post Jul 16 2019, 11:17 AM
Post#1


UtterAccess VIP
Posts: 13,369
Joined: 6-June 05
From: Dunbar,Scotland


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


--------------------
Hope this helps?

Mike

Get happiness out of your work or you may never know what happiness is.

Go to the top of the page
 
theDBguy
post Jul 16 2019, 11:22 AM
Post#2


Access Wiki and Forums Moderator
Posts: 76,317
Joined: 19-June 07
From: SunnySandyEggo


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.

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 
DanielPineault
post Jul 16 2019, 11:25 AM
Post#3


UtterAccess VIP
Posts: 6,854
Joined: 30-June 11



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?

--------------------
Daniel Pineault (2010-2019 Microsoft MVP)
Professional Help: http://www.cardaconsultants.com
Free MS Access Code, Tips, Tricks and Samples: http://www.devhut.net

* Design should never say "Look at me". It should always say "Look at this". -- David Craib
* A user interface is like a joke, if you have to explain it, it's not that good! -- Martin LeBlanc


All code samples, demonstration databases, links,... are provided 'AS IS' and are to be used at your own risk! Take the necessary steps to check, validate ...(you are responsible for your choices and actions)
Go to the top of the page
 
mike60smart
post Jul 16 2019, 11:45 AM
Post#4


UtterAccess VIP
Posts: 13,369
Joined: 6-June 05
From: Dunbar,Scotland


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

--------------------
Hope this helps?

Mike

Get happiness out of your work or you may never know what happiness is.

Go to the top of the page
 
theDBguy
post Jul 16 2019, 11:58 AM
Post#5


Access Wiki and Forums Moderator
Posts: 76,317
Joined: 19-June 07
From: SunnySandyEggo


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

--------------------
Just my 2 cents... "And if I claim to be a wise man, it surely means that I don't know" - Kansas
Access Website | Access Blog | Email
Go to the top of the page
 


Custom Search


RSSSearch   Top   Lo-Fi    21st September 2019 - 06:36 PM