Alright, I revised my code to generate the full file path rather than simply referring to the file in the attachment. To be clear, I was loading each document into the Access database into attachment fields for each record. I then attempted to use a DAO Recordset to gather the actual files and attach them that way. Now I use some module level variables to set the file path string for each order I am looking for, however I am still running into some problems here.
The code is below:
Code:
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim db As DAO.Database, rst As DAO.Recordset, strSQL As String
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
' Setting values to the below variables using the code in the subroutine EmailN
Call EmailN
With objOutlookMsg
' Send to the correct person
Set objOutlookRecip = .Recipients.Add("strRecip")
objOutlookRecip.Type = olTo
' Add the CC recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(strCC)
objOutlookRecip.Type = olCC
' Set the Subject, Body, and Importance of the message.
.Subject = strSubject
.Body = strMsg
.Importance = olImportanceHigh 'High importance
' Add attachments to the message.
Set db = CurrentDb
strSQL = "SELECT [MIPRs by Participant].[Fiscal Year], PartInfo.[Last Name], PartInfo.[Sponsoring Service]" & _
" FROM PartInfo INNER JOIN [MIPRs by Participant] ON PartInfo.[Smart Id] = [MIPRs by Participant].[SMART ID]" & _
" WHERE ((([MIPRs by Participant].[Fiscal Year])='" & FYear & "') AND ((PartInfo.[Sponsoring Service])='" & Service & "'));"
Set rst = db.OpenRecordset(strSQL)
Do While Not rst.EOF
sAttach1 = "\\comfort\SMART$\SMART\MIPRs\MIPR Memos\" & FYear & "\" & rst![Last Name] & " MIPR Memo Oct 2013.docx"
sAttach2 = "\\comfort\SMART$\SMART\MIPRs\SOWs\" & FYear & "\" & rst![Last Name] & " SOW Oct 2013.docx"
Set objOutlookAttach = .Attachments.Add(sAttach1)
Set objOutlookAttach = .Attachments.Add(sAttach2)
rst.MoveNext
Loop
rst.Close
Set db = Nothing
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
.Display
End With
Set objOutlook = Nothing
End Sub
Sub EmailN()
Dim db As DAO.Database, rstService As DAO.Recordset, rstAttach As DAO.Recordset, strLiaison As String
Dim strAttach As String, strPartList As String, sAttach1 As String, sAttach2 As String
Set db = CurrentDb
' Getting the liaison Email to make sure they are CC'd in the email to Nancy
strLiaison = "SELECT Email FROM ServiceLiasonInfo WHERE Service = '" & Service & "';"
Set rstService = db.OpenRecordset(strLiaison)
strCC = rstService![Email] & "; jlsimons@nps.edu" ' CC'ing liaison and Janna
strRecip = "nancy.seeger.ctr@osd.mil"
' Getting the Attachments for the emails to Nancy
strAttach = "SELECT PartInfo.[Sponsoring Service], [MIPRs by Participant].MIPRMemo," & _
" [MIPRs by Participant].SOW, [MIPRs by Participant].[Fiscal Year], PartInfo.[Participant Name]" & _
" FROM PartInfo INNER JOIN [MIPRs by Participant] ON PartInfo.[Smart Id] = [MIPRs by Participant].[SMART ID]" & _
" WHERE (((PartInfo.[Sponsoring Service])='" & Service & "')" & _
" AND (([MIPRs by Participant].[Fiscal Year])='" & FYear & "'));"
Set rstAttach = db.OpenRecordset(strAttach)
Do While Not rstAttach.EOF
strPartList = strPartList & vbCr & rstAttach![Participant Name]
rstAttach.MoveNext
Loop
' Writing the email - getting the subject line
strSubject = Service & " MIPR Initiating Documents for " & FYear
' Writing the email - getting the body message
strMsg = "Nancy," & vbCr & vbCr & "Attached are the " & FYear & " MIPR Documents for all " & _
Service & " participants. Below is a list of the " & Service & " RT participants. If any participants" & _
" are listed below but do not have documents attached, please let me know." & vbCr & vbCr & _
Service & " Participants:" & vbCr & strPartList & vbCr & vbCr & _
"Please contact me if you have any questions or concerns regarding these documents or anything MIPR related" & _
vbCr & vbCr & strSig
rstService.Close
Set rstService = Nothing
rstAttach.Close
Set rstAttach = Nothing
db.Close
Set db = Nothing