Barry McCocinner
New member
- Local time
- Today, 06:43
- Joined
- Apr 3, 2019
- Messages
- 7
Hello all,
I need a second set of eyes or someone that knows VBA better than I. I have a form that when a user selects "yes" generates a report. The report gets displayed as was as saved. On the report I have put an "Email report" button so that once the user verifies the information is correct they can just click and away it goes. The problem is the file name is dynamic. It works fine when generating the report, but when it comes to attaching the report to the email I get a "File not found" error even though the file is there. I've include code for the form and the report. :banghead:
Form VBA for generating and saving report
Private Sub CORLReq_AfterUpdate()
On Error GoTo CORLReq_AfterUpdate_Err
Dim stVendor As String
stVendor = "[VendorName]"
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "VSRA Request", acViewReport, , "[tblprescreen.ID]=" & [tblPrescreen.ID]
DoCmd.OutputTo acOutputReport, , acFormatPDF, "E:\reports\test" & [VendorName] & " Assessment Request.pdf", False, "", , acExportQualityPrint
CORLReq_AfterUpdate_Exit:
Exit Sub
CORLReq_AfterUpdate_Err:
MsgBox Error$
Resume CORLReq_AfterUpdate_Exit
End Sub
------------------------------------------------------------------------------
Report VBA for sending email
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Dim strPath As String
Dim strFilter As String
Dim strFile As String
Dim stVendor As String
stVendor = "[VendorName]"
strPath = "E:\reports\test" & [VendorName] & " Assessment Request" 'Edit to your path
strFilter = ".pdf"
strFile = Dir(strPath & strFilter)
If strFile <> "" Then
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatRichText
.To = "testuser@gmail.com"
''.cc = ""
''.bcc = ""
.Subject = "Request for " & [VendorName]
.HTMLBody = "We are requesting an assessment for " & [VendorName] & ". Please let us know if there are any questions."
.Attachments.Add (strPath & strFile)
'.Send
.Display 'Used during testing without sending (Comment out .Send if using this line)
End With
Else
MsgBox "No file matching " & strPath & strFilter & " found." & vbCrLf & _
"Processing terminated."
Exit Sub 'This line only required if more code past End If
End If
End Sub
I need a second set of eyes or someone that knows VBA better than I. I have a form that when a user selects "yes" generates a report. The report gets displayed as was as saved. On the report I have put an "Email report" button so that once the user verifies the information is correct they can just click and away it goes. The problem is the file name is dynamic. It works fine when generating the report, but when it comes to attaching the report to the email I get a "File not found" error even though the file is there. I've include code for the form and the report. :banghead:
Form VBA for generating and saving report
Private Sub CORLReq_AfterUpdate()
On Error GoTo CORLReq_AfterUpdate_Err
Dim stVendor As String
stVendor = "[VendorName]"
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "VSRA Request", acViewReport, , "[tblprescreen.ID]=" & [tblPrescreen.ID]
DoCmd.OutputTo acOutputReport, , acFormatPDF, "E:\reports\test" & [VendorName] & " Assessment Request.pdf", False, "", , acExportQualityPrint
CORLReq_AfterUpdate_Exit:
Exit Sub
CORLReq_AfterUpdate_Err:
MsgBox Error$
Resume CORLReq_AfterUpdate_Exit
End Sub
------------------------------------------------------------------------------
Report VBA for sending email
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Dim strPath As String
Dim strFilter As String
Dim strFile As String
Dim stVendor As String
stVendor = "[VendorName]"
strPath = "E:\reports\test" & [VendorName] & " Assessment Request" 'Edit to your path
strFilter = ".pdf"
strFile = Dir(strPath & strFilter)
If strFile <> "" Then
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
.BodyFormat = olFormatRichText
.To = "testuser@gmail.com"
''.cc = ""
''.bcc = ""
.Subject = "Request for " & [VendorName]
.HTMLBody = "We are requesting an assessment for " & [VendorName] & ". Please let us know if there are any questions."
.Attachments.Add (strPath & strFile)
'.Send
.Display 'Used during testing without sending (Comment out .Send if using this line)
End With
Else
MsgBox "No file matching " & strPath & strFilter & " found." & vbCrLf & _
"Processing terminated."
Exit Sub 'This line only required if more code past End If
End If
End Sub