VBA Email Coding Issue

poohbear2012

Registered User.
Local time
Today, 20:36
Joined
Jan 8, 2012
Messages
30
Hi

I am trying to write coding to enable the user to click on a button to send an email including fields from the active record.

I am currently experiencing issues with the lines highlighted in bold. It is coming up with the following error:

Compile Error
Expected: Line number or label or statement or end of statement.

Am not sure how to correct this as the references to the fields are correct and have tried different versions of line breaking without success.

Please advise what I am doing wrong...


Private Sub btnsendemail_Click()
If IsNull(Me.txtcount) Then
MsgBox "There are no appointments to email"
Else
On Error GoTo handdler
Dim objOL As Outlook.Application
Set objOL = New Outlook.Application
Dim ObjOLMsg As Outlook.MailItem
Set ObjOLMsg = Olk.CreateItem(olMailItem)
With ObjOLMsg
Dim ObjOLRecip As Outlook.Recipient
Set ObjOLRecip = .Recipients.Add(joebloggs@google.com)
ObjOLRecip.Type = olCC
.Subject = "Appointment Reminder"
.Body = "Thank you for your referral regarding" & Nz(Me.Forename, "") & "& Nz (Me.Surname,"")_"
& vbCrLF & "We have booked an outpatient appointment for them to be seen in" & Nz (Me.Consultant,"")& "clinic on" & Nz(Me.1st_Appointment,"") "at insert time." _
& vbCrLF & "Kind Regards" _
& vbCrLF & "The Appointment Team."_
& vbCrLF & "This message has been automatically generated by the Booking Database System"

End If
handdler:
If Err.Number = 2501 Then
MsgBox "You have cancelled the request", vbInformation
Else
Exit Sub
End If
End Sub

Regards
Trisha:confused:
 
Your firstline of the body is a bit messy, please use code tags the next time you post some code. It wil enhance the readability as the identing as wel as leading and trailing spaces are kept between the code tags.
I prefer the use of vbNewline at the end of a line, to me it's easier to read the code.
Code:
'.Body = "Thank you for your referral regarding" & Nz(Me.Forename, "") & "& Nz (Me.Surname,"")_[COLOR="Red"]"[/COLOR]
.Body = "Thank you for your referral regarding" & Nz(Me.Forename, "") & [COLOR="red"]" [/COLOR]" & Nz(Me.Surname,"") & vbnewline & _
"We have booked an outpatient appointment for them to be seen in" & Nz (Me.Consultant,"") & " clinic on " & Nz(Me.1st_Appointment,"") " at insert time." & vbnewline & _
"Kind Regards" & vbnewline & _
"The Appointment Team." & vbnewline & _
"This message has been automatically generated by the Booking Database System"
 
Hi Peter

Thank you for the prompt response, but I am still encountering the same error message when using this suggestion.
 
Your continuation set up on the first line of .Body is incorrect. There should be a ( _)

That's space and underscore character just like in your highlighted lines. And as I reread your post I think there may be other syntax issues.
 
I have since reviewed my entire syntax and have managed to correct the errors. Thank you for your assistance in pointing me in the right direction.
Regards
Trisha
 

Users who are viewing this thread

Back
Top Bottom