daveblanch
Registered User.
- Local time
- Today, 20:36
- Joined
- Aug 3, 2010
- Messages
- 11
Hi,
I have contacts database that is going to be used to send emails (using Outlook) using VBA, occasionally there will be the need to add attachments.
The form has a textbox in which the path to the document to be sent is entered.
All is well whenever an email is sent with an attachment, but when an email is to be sent without an attachment my code falls over stating 'Could not complete the operation. One or more parameter values are not valid"
I don't know how to ignore the .Attachments.Add code if the text box has no value entered. I've tried using Nz but I'm not sure if I'm using it correctly.
Hoping someone can help?
Thanks.
I have contacts database that is going to be used to send emails (using Outlook) using VBA, occasionally there will be the need to add attachments.
The form has a textbox in which the path to the document to be sent is entered.
All is well whenever an email is sent with an attachment, but when an email is to be sent without an attachment my code falls over stating 'Could not complete the operation. One or more parameter values are not valid"
I don't know how to ignore the .Attachments.Add code if the text box has no value entered. I've tried using Nz but I'm not sure if I'm using it correctly.
Hoping someone can help?
Code:
Private Sub btnEmail_Click()
Dim Email As String
Dim Subject As String
Dim Notes As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem
Dim sAttachment1 As String
Dim sAttachment2 As String
Dim sAttachment3 As String
DoCmd.GoToRecord , , acFirst
Do While Not Recordset.EOF
If Me.Labels = 0 Then
Recordset.MoveNext
Else
Email = Me!Email
Subject = Me!txtSubject
Notes = Me!txtBody
Attachment1 = Nz(Me!txtAtt1)
Attachment1 = Me!txtAtt1
Attachment2 = Me!txtAtt2
Attachment3 = Me!txtAtt3
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)
With objEmail
.To = Email
.Subject = Subject
.Body = Notes & vbNewLine & vbNewLine & "Signature"
.Attachments.Add Attachment1
.Attachments.Add Attachment2
.Attachments.Add Attachment3
.Send
End With
Recordset.MoveNext
End If
Loop
Set objEmail = Nothing
MsgBox "Your eMails have been sent, please close the close button on the form."
End Sub
Thanks.