Hi Team, I use the below code to attach the Access Report in the email. It was working fine earlier, but now I'm getting Run Time Error "cannot find this file verify the path"
I'm facing the issue when the attachment is added (Attachments.add filename).
Please help.
I'm facing the issue when the attachment is added (Attachments.add filename).
Code:
Private Sub Send_Mail_Click()
Dim Rptname As String
Rptname = Me.cmb_ReportName.Value
Dim oApp As New Outlook.Application
Dim oEmail As Outlook.MailItem
Dim fileName As String, todayDate As String
Dim Subjectline As String, s As String, i As Long, j As Long
Dim Bodyline As String
Dim UserNames As String
'Export report in same folder as db with date stamp
todayDate = Format(Date, "MMDDYYYY")
If Left(Rptname, 3) = "Rpt" Then
fileName = Replace(Trim(Mid(Rptname, 5, 100)), "_", " ") & " Report - " & todayDate & ".pdf"
i = InStr(Rptname, "_")
j = InStrRev(Rptname, "_")
s = Mid(Rptname, i + 1, j - i - 1)
Subjectline = s
Else:
fileName = Rptname & " Report - " & todayDate & ".pdf"
End If
If Rptname = "Rpt_Overall_Summary" Or Rptname = "Rpt_Summary_Overview" Then
i = InStr(Rptname, "_")
'j = InStrRev(Rptname, "_")
s = Mid(Rptname, i + 1)
s = Replace(s, "_", " ")
Subjectline = s
End If
On Error Resume Next
UserNames = CreateObject("excel.application").UserName
On Error GoTo 0
If Form_Navigator_Form.FilterbyYear.Value <> "" Then
Subjectline = "F&A OE - " & Subjectline & " - " & Form_Navigator_Form.FilterbyYear.Value
Bodyline = "Hi Team," & vbNewLine & vbNewLine & _
"Hope you are doing good and safe." & vbNewLine & vbNewLine & _
"Attached is the " & s & " - Report - " & Form_Navigator_Form.FilterbyYear.Value & "." & vbNewLine & vbNewLine & _
"Thank you." & vbNewLine & vbNewLine & _
"Regards," & vbNewLine & _
UserNames
Else:
Subjectline = "F&A OE - " & Subjectline & " - " & "Inception till date"
Bodyline = "Hi Team," & vbNewLine & vbNewLine & _
"Hope you are doing good and safe." & vbNewLine & vbNewLine & _
"Attached is the " & s & " - Report - Inception till date." & vbNewLine & vbNewLine & _
"Thank you." & vbNewLine & vbNewLine & _
"Regards," & vbNewLine & _
UserNames
End If
DoCmd.OutputTo acReport, Rptname, acFormatPDF, fileName, False
'Email the results of the report generated
Set oEmail = oApp.CreateItem(olMailItem)
With oEmail
.Recipients.Add Me.txt_MailIDs
.Subject = Subjectline 'Me.txt_MailSubject 'Trim(Mid(Rptname, 5, 100)) & " Report - Dated - " & Format(Date, "MM/DD/YYYY")
.Body = Bodyline
.Attachments.Add fileName 'I'M FACING ISSUE HERE'
'.Send
.display
End With
MsgBox "Report has been successfully drafted. Request you to please check and send." & vbNewLine & vbNewLine & "Thank you", vbInformation + vbOKOnly, "EMAIL STATUS"
'DoCmd.Close acForm, Me.Name
End Sub
Please help.