.Attachments in visual basic

Slayboy

Registered User.
Local time
Today, 23:51
Joined
Apr 28, 2004
Messages
28
I have some code that sets up some emails from Access to Outlook, but I want to add an attachment to each email. I have this code below and it works, the only problem is the new line that I have put in: .Attachments = "P:\...
It causes a run time error and I can't work out why, any ideas?

Sub MattSendMessage(Name As String, OutlookName As String, MessageTitle As Variant, Message As Variant)

Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
' Add the recipient to the email.
Set objOutlookRecip = .Recipients.Add(OutlookName)
objOutlookRecip.Type = olTo
' Set the subject, body and importance of the email.
.Subject = MessageTitle
.Body = Message
.Importance = olImportanceNormal
.Attachments = "P:\Corporate Audit Database\Recs Downloads Database\2008\G July 08\Sent to business\Amanda Taylor.html"
' Resolve the email address name.
objOutlookRecip.Resolve

' Only display the mail message, not send.
.Display
End With

End Sub
 
You need to use the "add" method to add an attachment:

Code:
.Attachments.add = "P:\Corporate Audit Database\Recs Downloads Database\2008\G July 08\Sent to business\Amanda Taylor.html"
 
It still gives me a runtime error, any other suggestions?
 
Remove the "=" so it reads:

Code:
.Attachments.add  "P:\Corporate Audit Database\Recs Downloads Database\2008\G July 08\Sent to business\Amanda Taylor.html"
 

Users who are viewing this thread

Back
Top Bottom