Help! For the life of me I cannot figure out what I'm doing wrong. I have a Microsoft Access query that lists the full directory path for a number of files. I tried to write a script that will create a new email message in Outlook that attaches each of the files listed in the query. The user will then input the recipient and subject, and can send the email from Outlook.
The script works great, EXCEPT that it repeats the same attachment. For example, the query might return 3 files. Instead of inserting each of the 3 files as an attachment, it inserts the first file 3 times. This is obviously a problem with my loop below, but I can't figure out. Can anyone tell me what I'm doing wrong? Thanks in advance!!!
------------------
Private Sub EmailMarkedDocuments_Click()
Dim MyDB As Database
Dim MyRS As Recordset
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookAttach As Outlook.Attachment
Dim TheAttachment As String
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(0) 'olMailItem
Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset("CheckForMarkedQuery")
TheAttachment = MyRS![FilePath]
MyRS.MoveFirst
With objOutlookMsg
Do Until MyRS.EOF
MyRS.MoveNext
Set objOutlookAttach = .Attachments.Add(TheAttachment)
Loop
End With
objOutlookMsg.Display
End Sub
The script works great, EXCEPT that it repeats the same attachment. For example, the query might return 3 files. Instead of inserting each of the 3 files as an attachment, it inserts the first file 3 times. This is obviously a problem with my loop below, but I can't figure out. Can anyone tell me what I'm doing wrong? Thanks in advance!!!
------------------
Private Sub EmailMarkedDocuments_Click()
Dim MyDB As Database
Dim MyRS As Recordset
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookAttach As Outlook.Attachment
Dim TheAttachment As String
Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(0) 'olMailItem
Set MyDB = CurrentDb
Set MyRS = MyDB.OpenRecordset("CheckForMarkedQuery")
TheAttachment = MyRS![FilePath]
MyRS.MoveFirst
With objOutlookMsg
Do Until MyRS.EOF
MyRS.MoveNext
Set objOutlookAttach = .Attachments.Add(TheAttachment)
Loop
End With
objOutlookMsg.Display
End Sub