VBA - Sending Multiple Attachments to Outlook message

L

lgray

Is there anybody out there that can go through this simple code and see what
I've done wrong? I'm still novice, trying to do something new... I'm trying
to go through a recordset to attach files; where a table holds the path to
the attachment. The outlook attachment part of the code works when I hard
code the path into the script, but when I implement the recordset I get an
error that reads...

"Run-time error '-2147467259 (80004005)'
Can't create file: Test1.doc. Right-click the folder you want to create the
file in, and then click Properties on the shortcut menu to check your
permissions for the folder."

Where "Test1.doc" is the name of the file in the table. The table has a
field that holds the file path and the name of the document (again,
Test1.doc, in this example)

The code is below:
Sub createOutlookMailItem()

Dim EmailCode As Database
Dim rs As Recordset
Dim objOutlook As Outlook.Application
Dim newMail As MailItem


Set objOutlook = CreateObject("Outlook.application")
Set newMail = objOutlook.CreateItem(olMailItem)

Set newMailAttachments = newMail.Attachments
Set EmailCode = CurrentDb
Set rs = EmailCode.OpenRecordset("Encroachment_Attachments")

File1 = rs("FullPathToFile")
newMail.To = "(e-mail address removed)"
newMail.Subject = "Encroachment Documents"

While Not rs.EOF
newMailAttachments.Add File1, olByValue, 1, "Test Doc"

Wend

newMail.Display
newMail.Subject = "Documents Confirmation"
newMail.OriginatorDeliveryReportRequested = True
newMail.ReadReceiptRequested = True
newMail.Display

Set newMailAttachments = Nothing
Set newMail = Nothing
Set objOutlook = Nothing


End Sub
 
P

pietlinden

Is there anybody out there that can go through this simple code and see what
I've done wrong?  I'm still novice, trying to do something new...  I'm trying
to go through a recordset to attach files; where a table holds the path to
the attachment.  The outlook attachment part of the code works when I hard
code the path into the script, but when I implement the recordset I get an
error that reads...

"Run-time error '-2147467259 (80004005)'
Can't create file:  Test1.doc. Right-click the folder you want to create the
file in, and then click Properties on the shortcut menu to check your
permissions for the folder."

Where "Test1.doc" is the name of the file in the table.  The table has a
field that holds the file path and the name of the document (again,
Test1.doc, in this example)

The code is below:  
Sub createOutlookMailItem()

    Dim EmailCode As Database
    Dim rs As Recordset
    Dim objOutlook As Outlook.Application
    Dim newMail As MailItem

    Set objOutlook = CreateObject("Outlook.application")
    Set newMail = objOutlook.CreateItem(olMailItem)
'this is redundant... just use newMail.Attachments.Add(strFile)
    Set newMailAttachments = newMail.Attachments
    Set EmailCode = CurrentDb
    Set rs = EmailCode.OpenRecordset("Encroachment_Attachments")

THE NEXT LINE IS WRONG.
'     File1 = rs("FullPathToFile")
    newMail.To = "(e-mail address removed)"
    newMail.Subject = "Encroachment Documents"

    While Not rs.EOF
'--THIS DOES NOT MAKE SENSE EITHER
'         newMailAttachments.Add File1, olByValue, 1, "Test Doc"
'--DO THIS INSTEAD
newMail.Attachments.Add rs.Fields("FullPathToFile),
olByValue, 1, "Test Doc"
        Wend
'     newMail.Display '---should be last line in your code.
    newMail.Subject = "Documents Confirmation"
    newMail.OriginatorDeliveryReportRequested = True
    newMail.ReadReceiptRequested = True

'either put .Display here or .SEND
newMail.Send
 
L

lgray

Any ideas on this.....
The next Error message is: "Operation is not supported for this type of
object" and Highlights the following line:

newMail.Attachments.Add rs.Fields("FullPathToFile"), olByValue, 1, "Test Doc"

of the following Code:

Sub createOutlookMailItem()
Dim EmailCode As Database
Dim rs As Recordset
Dim objOutlook As Outlook.Application
Dim newMail As MailItem

Set objOutlook = CreateObject("Outlook.application")
Set newMail = objOutlook.CreateItem(olMailItem)

Set EmailCode = CurrentDb
Set rs = EmailCode.OpenRecordset("Encroachment_Attachments")

newMail.To = "(e-mail address removed)"
newMail.Subject = "Encroachment Documents"

While Not rs.EOF
newMail.Attachments.Add rs.Fields("FullPathToFile"), olByValue, 1, "Test
Doc"
Wend

newMail.Display
Set newMailAttachments = Nothing
Set newMail = Nothing
Set objOutlook = Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top