PDF Attachments

mike60smart

Registered User.
Local time
Today, 15:50
Joined
Aug 6, 2017
Messages
1,993
Hi Everyone

Can someone tell me where I an going wrong with the following Code to add an attachment to an email.

The Code creates a PDF file in the Folder as required
It displays the Report
But it does not Display the Email in Outlook ?

The code is as follows:-

Code:
Private Sub cmdPrint_Click()

10        On Error GoTo cmdPrint_Click_Error

20        If Me.AgentPaid = -1 Then
30            Me.PaidDate = Date

40            If Me.Dirty Then Me.Dirty = False
50            MsgBox "A Copy of the PDF will be saved to the C Drive EMails Folder", vbInformation
              Dim strSQL As String
              Dim outApp As Object
              Dim outMail As Object
              Dim strReportname As String
              Dim strMailItem As String
              Dim strWhere As String
              Dim strToWhom As String
              Dim strMsg As String
              Dim strSubject As String
              Dim strPath As String

60            strToWhom = Me.AgenteMailP
70            strWhere = "[ID]=" & Me.id
80            strSubject = "Settlement"
90            strReportname = "AgentSettlement"

100           strMsg = "Find attached latest Settlement Details"
110           If Dir("C:\Emails", vbDirectory) = "" Then MkDir "C:\Emails"

120           strPath = "C:\Emails\" & strReportname & "-" & Format(Date, "mmddyyyy") & "- Agent Settlement" & [LoadNumber] & ".pdf"
      
130           Set outApp = CreateObject("Outlook.Application")
140           Set outMail = outApp.CreateItem(0)

150           DoCmd.OpenReport "AgentSettlement", acViewPreview
160           DoCmd.OutputTo acOutputReport, "AgentSettlement", acFormatPDF, "C:\Emails\" & Format(Date, "mmddyyyy") & "- Agent Settlement" & [LoadNumber] & ".pdf", True 'True shows email before semding

170       End If
180       With outMail
190           .To = strToWhom
200           .Subject = strSubject
201           .Attachments.Add strPath
220           .Body = strMsg
230           .Display
240       End With



          
250       On Error GoTo 0
260       Exit Sub

cmdPrint_Click_Error:

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

End Sub

I am getting the following error.

Any help appreciated
 

Attachments

  • Error.png
    Error.png
    7.9 KB · Views: 119
strPath does not contain the file path used in OutputTo argument. Why don't you use the variable in OutputTo?
 
strPath does not contain the file path used in OutputTo argument. Why don't you use the variable in OutputTo?
Hi June
Many thanks that fixed it
 
strPath does not contain the file path used in OutputTo argument. Why don't you use the variable in OutputTo?
Hi June

One thing that is strange is that on running the code i get the following outputs:-

1. a Print Preview of theReport
2. a PDF file displayed in Microsoft Edge - even though my default browser is Google
3. an EMail

Any ideas why this would happen??
 
The comment at the end is not accurate, as it simply displays the newly created PDF in the default application set to open PDFs (which seems to be Edge; Docmd.OutputTo method has nothing to do with emailing, the email is being displayed by the .Display line):
DoCmd.OutputTo acOutputReport, "AgentSettlement", acFormatPDF, "C:\Emails\" & Format(Date, "mmddyyyy") & "- Agent Settlement" & [LoadNumber] & ".pdf", True 'True shows email before semding

Cheers,
 
The comment at the end is not accurate, as it simply displays the newly created PDF in the default application set to open PDFs (which seems to be Edge; Docmd.OutputTo method has nothing to do with emailing, the email is being displayed by the .Display line):
DoCmd.OutputTo acOutputReport, "AgentSettlement", acFormatPDF, "C:\Emails\" & Format(Date, "mmddyyyy") & "- Agent Settlement" & [LoadNumber] & ".pdf", True 'True shows email before semding

Cheers,
Hi Vlad

Many thanks That clears that issue up nicely
 

Users who are viewing this thread

Back
Top Bottom