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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Email Code HELP NEEDED

Status
Not open for further replies.

jmskarp

Technical User
Aug 6, 2002
27
US
I am using the following code to create and send an email. It works great.

My challenge is that I am trying to use the code again within the same db on a different form, but no luck. I get no response at all when I execute the Click event. I am using the same variables but assigning different fields to the variables. I tried renaming variable and object names but also, no luck.

I am not sure why it would not work, can someone advise.

Thanks.
Joel


CODE:

Private Sub cmdSendEmail_Click()
'Just a message to make sure the user is using a valid email address.
RetValue = MsgBox("1. Are you sure the Requestor's email address is valid?" & Chr(13) _
& "2. Have you entered a response to the requestor?", vbOKCancel)
If RetValue = 1 Then

'******begin code******
Dim email, ref, origin, destination, notes, Desc As String
Dim objOutlook As Object 'Outlook.Application
Dim objEmail As Object 'Outlook.MailItem
'
'**gathers information from your form. this sets the string variable to your fields

email = Me!email
ref = Me.Topic
notes = Me!notes
Desc = Me.Q4433.Value

'***creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

'***creates and sends email
With objEmail
.to = email
.Subject = ref '& " " & origin & " " & destination
.Body = "1. Message from SDG POC: " & notes & Chr(13) _
& "2. Description from Request Form: " & Desc
.Send
End With
'
'**closes outlook
Set objEmail = Nothing
'The next line is commented out to eliminate the closing of MS Outlook
'I have commented the following line out to prevent outlook from closing.
'objOutlook.Quit
'
Exit Sub
'****end code****

Else
Exit Sub
End If

End Sub

 
Can you post the original code that is working? I'm assuming that the code you posted was the one that you already altered. It would be easier for us to compare the 2.
 
kphu,


Thank you for responding. I have identified another means to accomplish my email issue.

Here is the new code:

'This works .....DoCmd.SendObject acSendNoObject, "", acFormatTXT, "ToEmailAddress","CcEmailAddress" ,"BccEmailAddress" , "Subject Here", "message", True


The original old code was.....

'******begin code******
Dim email, ref, origin, destination, notes As String
Dim objOutlook As Outlook.Application
Dim objEmail As Outlook.MailItem

'**gathers information from your form. this sets the string variable to your fields
email = Me!email
ref = Me!ref
origin = Me!origin
destination = Me!destination
notes = Me!notes

'***creates an instance of Outlook
Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

'***creates and sends email
With objEmail
.To = email
.Subject = ref & " " & origin & " " & destination
.Body = notes
.Send
End With

'**closes outlook
Set objEmail = Nothing
objOutlook.Quit

Exit Sub
'****end code****
 
skarpjm,

glad you figured it out.

that always happens to me. I post something and then figure it out a minute later.
 
since i borrowed some of your info for this...
Just had to share this...
It works fine with err handling from null attachments and null CC's in case they want to email without a cc or attach, and im requiring the subj and body.
Make sure you have Outlook and DAO references checked.
It may seem odd to call variables then use the me.name, but it had probs use vars, maybe someone can point out why.
**********
Private Sub sendEmailBtn_Click()
On Error GoTo Err_sendEmail
Dim db As DAO.Database, rst As DAO.Recordset
Dim strBody, subject, email As String
Dim varCC, varAtt As Variant
Dim objOutlook As Object 'Outlook.Application
Dim objEmail As Object 'Outlook.MailItem

email = Me.emailTo
varCC = Me.emailCC
subject = Me.emailSubj
strBody = Me.emailBody
varCC = Me.emailCC
varAtt = Me.Text14

If IsNull(Me.emailSubj) Then
MsgBox "There must be something in the subject line for proper email functionality"
Exit Sub
End If
If IsNull(Me.emailBody) Then
MsgBox "There must be something in the body for proper email functionality"
Exit Sub
End If
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70

Set db = CurrentDb
Set rst = db.OpenRecordset("tblHistory")
rst.AddNew

rst!HistType = "Email"
rst!HistNotes = Me.emailSubj
rst!HistDetails = strBody
rst!HistDate = Format(Now(), "mm/dd/yyyy")
rst!HistTime = Format(Time(), "hh:mm")
rst!ContactID = Me.ContactID.Value
rst!HistAttachFile = Me.Text14.Value

rst.Update
rst.Close
db.Close

Set objOutlook = CreateObject("Outlook.application")
Set objEmail = objOutlook.CreateItem(olMailItem)

With objEmail
.to = email
.subject = subject
.Body = strBody
If IsNull(varCC) And IsNull(varAtt) Then
.Send
ElseIf IsNull(varAtt) Then
.CC = varCC
.Send
Else
.Attachments.Add varAtt, olByValue, 1
.Send
End If
End With

Set objEmail = Nothing

'objOutlook.Quit

Forms![Contacts].[subHistory].Requery
DoCmd.Close acForm, Me.Name

Err_sendEmail:
Exit Sub
End Sub
 
Adam,

Thanks for the information. I am still experimenting with this whole outlook stuff, but I have fiqured it out atleast partially.

I give you a star for the post.

Have a Great New Year!

Joel



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top