Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Outlook Attachment olByValue olByReference Problem

Status
Not open for further replies.

fiona108

Programmer
Jun 24, 2004
7
GB
I have written VBA code in Access to create an email, with an attachment, and display it so the user can check, make final adjustments, then Send. However, I am having problems with the attachment. If I use olByValue, I get the following error message :

‘Unable to perform the operation. Could not open the item. Try again.’

I know that I have got the string correct for the location of the file because if I replace olByValue with olBReference, the email is displayed with the correct link. However, when I attempt to send the email, I get the same error message as listed above. My client doesn't get any error message, but the attachment is not sent to the recipient.

All help gratefully received - Thanks.

Here is the code :

Private Function SendEMail(addressee, Subject, Body) As Boolean

Dim oOutlookApp As Outlook.Application
Dim oMailItem As Outlook.MailItem
Dim strAttachment As String

On Error GoTo SendEmailError

strAttachment = "d:\test.doc"

Set oOutlookApp = CreateObject("Outlook.Application")
Set oMailItem = oOutlookApp.CreateItem(olMailItem)

oMailItem.Recipients.Add (addressee)
oMailItem.Subject = Subject
oMailItem.Body = Body
oMailItem.Attachments.Add strAttachment, olByValue, 1, "TEST" ‘fails
‘oMailItem.Attachments.Add strAttachment, olByReference ‘works a bit
oMailItem.Save

oMailItem.Display
SendEMail = True

SendEmailExit:
Set oMailItem = Nothing
Set oOutlookApp = Nothing
Exit Function

SendEmailError:
MsgBox "Error"
SendEMail = False
GoTo SendEmailExit

End Function
 
Why not (?):

[tt] [blue]oMailItem.To[/blue] = addressee
oMailItem.Subject = Subject
oMailItem.Body = Body
[blue]oMailItem.Attachments.Add strAttachment[/blue][/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top