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
‘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