Generate email body with values from MS Access userform - vba

I created a form and would like to send an email using a button capturing data from the sub-form (based on a query).
I am using this YouTube video as a guide and get stuck (starting from msg =....
Private Sub cmd_EmailContact_Click()
Dim Msg As String
msg = "Dear " & First name & ",<P>" & _
Student First name & "has been successfully been loaded on the platform" & ",<P>" & _
"Student login details on the platform are:" & ",<P>" & _
"Username:" & Username & ",<P>" & _
"Password:" & Password**
Dim O As Outlook.Application
Dim M As Outlook.MailItem
Set O = New Outlook.Application
Set M = O.CreateItem(olMailItem)
With M
.BodyFormat = olFormatHTML
.HTMLBody = Msg
.To = Email
.Subject = "Student available on OARS"
.Display
End With
Set M = Nothing
Set O = Nothing
End Sub
Variables are populated on a query on the form.
First name (Name of teacher)
Student First name
Username
Password

To send the email as HTML, you will need to format the body with HTML tags and set the HTMLBody property of the olMailItem (email) as shown below.
The example uses late binding (no reference to Outlook needed) which means it can run with different versions of outlook installed.
Private Sub cmd_EmailContact_Click()
Dim firstName As String, _
studentFirstName As String, _
userName As String, _
password As String, _
email As String, _
body_ As String
'provide values
firstName = "ABC"
studentFirstName = "XYZ"
userName = "User"
password = "Pass"
email = "foo#bar.com"
'build body
body_ = "<p> Dear" & firstName & ", </p>" _
& "<p>" & studentFirstName & " has been successfully been loaded on the platform. </p>" _
& "<p> Student login details on the platform are: </p>" _
& "<p> Username: " & userName & "</p>" _
& "<p> Password: " & password & "</p>"
'send email
With CreateObject("Outlook.Application")
With .CreateItem(0) 'olMailItem
.BodyFormat = 2 'olFormatHTML
.To = email
.Subject = "Student available on OARS"
.HTMLBody = "<html><head></head><body>" & body_ & "</body></html>"
.Display
End With
End With
End Sub
You will need to provide values for the following variables:
FirstName
StudentFirstName
UserName
Password
Email

Related

populate an email with information from an access form, how to have the referrenced tables selected option show on email, not primary key number

I've created a button within a form that will populate an email with certain fileds. some of these fields are drop down boxes which get their information from another table. In this example I have an field for the title (Mr, Mrs, etc), the form shows Mr but when this is written to an email the primary key is there in place of the actual title. "1" instead of "Mr.".
I've tried using what seems logical to get the info from the "TitleTBL"(name of table) but I don't seem to be getting it right. please see the VBA code below and let me know how to reference the actual Title and not the primary key of that option.
Thank You
Private Sub TransferDanR_Click()
Dim ID As Variant
Dim Title As Variant
Dim First As Variant
Dim Last As Variant
Dim Addr1 As Variant
Dim Addr2 As Variant
Dim Postcode As Variant
Dim HomePhone As Variant
Dim MobilePhone As Variant
Dim Insurer As Variant
Dim RenewalDate As Variant
Dim PolicyNotes As Variant
Dim ContactNotes As Variant
Dim LGAgent As Variant
Dim objOutlook As Object
Dim objEmail As Object
ID = Forms("DataToDialFRM").ID
Title = Forms("DataToDialFRM").Title
First = Forms("DataToDialFRM").First
Last = Forms("DataToDialFRM").Last
Addr1 = Forms("DataToDialFRM").Addr1
Addr2 = Forms("DataToDialFRM").Addr2
Postcode = Forms("DataToDialFRM").Postcode
HomePhone = Forms("DataToDialFRM").HomePhone
MobilePhone = Forms("DataToDialFRM").MobilePhone
Insurer = Forms("DataToDialFRM").Insurer
RenewalDate = Forms("DataToDialFRM").RenewalDate
PolicyNotes = Forms("DataToDialFRM").PolicyNotes
ContactNotes = Forms("DataToDialFRM").ContactNotes
LGAgent = Forms("DataToDialFRM").LGAgent
Set objOutlook = CreateObject("Outlook.Application")
Set objEmail = objOutlook.CreateItem(0)
With objEmail
.To = "emailaddress; emailaddress; emailaddress"
.Subject = ID & " " & Last & " from " & LGAgent & " (Automated Transfer Email)"
.HTMLBody = "<p>" & "Name: " & Title & ", " & First & ", " & Last & "<p>" & "Address: " & Addr1 & ", " & Addr2 & ", " & Postcode & "<p>" & "HomePhone: " & HomePhone & "<p>" & "MobilePhone: " & MobilePhone & "<p>" & "Insurer " & Insurer & "<p>" & "Renewal Date: " & RenewalDate & "<p>" & "Policy Notes: " & "<p>" & PolicyNotes & "<p>" & "Contact Notes: " & "<p>" & ContactNotes
.Display
End With
Set objEmail = Nothing
Set objOutlook = Nothing
End Sub
To retrieve the display value of a combobox you use the .Column property.
E.g. if your Title combobox has 2 columns with TitleID ("1") and Title ("Mr."), you need .Column(1) because the column index is zero-bound.
You can make your code better readable by using a With statement:
With Forms("DataToDialFRM")
ID = !ID
Title = !Title.Column(1)
First = !First
' ...
End With
Use bang ! to refer to form controls, not form recordsource values.

How to pass email address and password for creating emails in GMail?

I need my Access Database to create emails that are sent at the press of a button.
This works for Outlook, and I adapted the code for Gmail.
I don't want to hardcode the email username and password. I want to pick it up from a combobox on the main form.
I get the error
Private Sub Email_Allocation_List_Click()
Dim newMail As CDO.Message
Dim mailConfiguration As CDO.Configuration
Dim fields As Variant
Dim msConfigURL As String
On Error GoTo errHandle
Set newMail = New CDO.Message
Set mailConfiguration = New CDO.Configuration
mailConfiguration.Load -1
Set fields = mailConfiguration.fields
With newMail
.Subject = "subject"
.From = [Forms]![Main form]![EmailAddress].Column(1)
.To = "email address"
.CC = "email address"
.BCC = ""
.TextBody = "Hello, " & vbNewLine & vbNewLine & _
"Please find attached todays list of lines to be allocated." & _
vbNewLine & vbNewLine & "Kind Regards." & vbNewLine & vbNewLine & "Carly"
.AddAttachment "file location"
End With
msConfigURL = "http://schemas.microsoft.com/cdo/configuration"
With fields
.Item(msConfigURL & "/smtpusessl") = True
.Item(msConfigURL & "/smtpauthenticate") = 1
.Item(msConfigURL & "/smtpserver") = "smtp.gmail.com"
.Item(msConfigURL & "/smtpserverport") = 465
.Item(msConfigURL & "/sendusing") = 2
.Item(msConfigURL & "/sendusername") = [Forms]![Main form]![EmailAddress].Column(1)
.Item(msConfigURL & "/sendpassword") = [Forms]![Main form]![EmailAddress].Column(2)
.Update
End With
newMail.Configuration = mailConfiguration
newMail.Send
MsgBox "E-Mail has been sent", vbInformation
exit_line:
'// Release object memory
Set newMail = Nothing
Set mailConfiguration = Nothing
Exit Sub
errHandle:
MsgBox "Error: " & Err.Description, vbInformation
GoTo exit_line
End Sub
I checked the comboboxes work with a text box.
After a lot of messing around, you are correct, the code is fine - and it was reading the contents of the combo box correctly - however GMail didn't accept the log in information if there was a capital letter on the email address!!! Now working beautifully.

How can I automatically send email from Thunderbird with Excel VBA?

What I want to do is send an email from a Thunderbird account automatically. The user shouldn't even have to hit the Send button of the email.
I've tried using CDO, but the problem with it is that you have to input the username and password of the account you are sending from. This macro will be used from several different accounts, so inputting each username and password isn't feasible. I could use CDO if there was someway of retrieving the username, password, and smtp server from Thunderbird, but I feel like the code I already have should be able to accomplish this without CDO (hopefully).
Here is really the only code I see (and it's everywhere) in regards to accomplishing this.
Sub Thunderbird()
Dim thund As String
Dim email As String
Dim cc As String
Dim bcc As String
Dim subj As String
Dim body As String
email = "email#test.com"
cc = "cc#test.com"
bcc = "bcc#test.com"
subj = "Subject"
body = "body text"
thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe"
thund = thund & " -compose " & Chr$(34) & "mailto:" & email & "?"
thund = thund & "cc=" & Chr$(34) & cc & "?"
thund = thund & "bcc=" & Chr$(34) & bcc & "?"
thund = thund & "subject=" & Chr$(34) & subj & Chr$(34)
thund = thund & "body=" & Chr$(34) & body
Call Shell(thund, vbNormalFocus)
SendKeys "^+{ENTER}", True
End Sub
As of right now, the fields cc, bcc, subj, and body are all recognized correctly. The problem is they all get added to the end of the first field. For instance, with the way the code is right now, cc will get put in the cc field, but bcc, subj, and body all get appended to cc in the cc field of Thunderbird.
If I comment cc out, then bcc is put in the correct field, but subj and body get appended to bcc in the bcc field of Thunderbird.
If I comment cc and bcc out, then subj gets put in the correct field, but body gets appended to subj in the subject field of Thunderbird.
So basically I need to add the correct code at the end of each of these lines. I've tried both "?" and Chr$(34) to no avail.
Lastly, SendKeys "^+{ENTER}", True isn't working at all. This might be because of all the parameters not being put in the correct field of Thunderbird, but not sure since I can't get that working. Email from Thunderbird displays, but this code isn't sending the email like it's supposed to.
SOLUTION (as provided by #zedfoxus)
Sub Thunderbird()
Dim thund As String
Dim email As String
Dim cc As String
Dim bcc As String
Dim subj As String
Dim body As String
email = "email#test.com"
cc = "cc#test.com"
bcc = "bcc#test.com"
subj = "Subject"
body = "body text"
thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe" & _
" -compose " & """" & _
"to='" & email & "'," & _
"cc='" & cc & "'," & _
"bcc='" & bcc & "'," & _
"subject='" & subj & "'," & _
"body='" & body & "'" & """"
Call Shell(thund, vbNormalFocus)
Application.Wait (Now + TimeValue("0:00:03"))
SendKeys "^{ENTER}", True
End Sub
You were pretty close. Try this:
Public Sub SendEmail()
Dim thund As String
Dim email As String
Dim cc As String
Dim bcc As String
Dim subj As String
Dim body As String
email = "test#test.com"
cc = "test#test.com"
bcc = "test#test.com"
subj = "Testing"
body = "Testing"
thund = "C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe " & _
"-compose " & """" & _
"to='" & email & "'," & _
"cc='" & cc & "'," & _
"bcc='" & bcc & "'," & _
"subject='" & subj & "'," & _
"body='" & body & "'" & """"
Call Shell(thund, vbNormalFocus)
SendKeys "^+{ENTER}", True
End Sub
Notice the example from http://kb.mozillazine.org/Command_line_arguments_(Thunderbird).
thunderbird -compose "to='john#example.com,kathy#example.com',cc='britney#example.com',subject='dinner',body='How about dinner tonight?',attachment='C:\temp\info.doc,C:\temp\food.doc'"
The example indicates that after -compose we should use type our information in double-quotes. Each parameter is separated by comma. Nomenclature is parameter='value[,value]' [,parameter='value[,value]]....

How do send a link to a document using word vba CDO email process?

Does anybody know how to send a link to a word document via email in word vba? I want to use gmail not outlook. I found a solution for outlook:
http://www.rondebruin.nl/win/s1/outlook/bmail10.htm
Is there anyway that can be modified to work using gmail?
I modified: http://www.rondebruin.nl/win/s1/cdo.htm to work with gmail, and it is working fine. I just need to add a link to the body of the email.
You'll need to use the htmlBody property instead of TextBody and use an <a> tag in the HTML.
I didn't know how to send my code...So I posted it as an answer. Hope that's ok. Also I get a strange run time error that I wasn't getting before. Other than that it works.
Thanks!
Danielle
Option Explicit ' Modified for dSavage
'If you have a GMail account then you can try this example to use the GMail smtp server
'The example will send a small text message
'You must change four code lines before you can test the code
'.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
'.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"
'Use your own mail address to test the code in this line
'.To = "Mail address receiver"
'Change YourName to the From name you want to use
'.From = """YourName"" "
'If you get this error : The transport failed to connect to the server
'then try changing the SMTP port from 25 to 465
Sub CDO_Mail_Example()
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim sAddForm As String
Dim sForm As String
Dim Flds As Variant
Dim iSMTP_Port As Integer
Dim sFirstReviewer As String
Dim sUserName As String
Dim sGmailPassword As String
sFirstReviewer = Range("F4").Value
sUserName = Range("F6").Value & "#indicate1.com"
sGmailPassword = Range("F8").Value
iSMTP_Port = Range("F10").Value '25 at Indicate; 465 away.
sAddForm = Range("I12").Value
'sForm = Range("F4").Value
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
If sAddForm = "Yes" Then
sForm = "Z:\Quality# # Document_Development# Documents_for_Review\12002-01-01 Company Handbook.doc"
Else
sForm = ""
End If
Debug.Print "sForm = " & sForm ' *******************************************
Debug.Print "sUserName = " & sUserName ' *******************************************
iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = sUserName
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Micro5cope"
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = iSMTP_Port '25 at Indicate; 465 away.
.Update
End With
strbody = "To " & sFirstReviewer & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4" & vbNewLine & vbNewLine & vbNewLine & _
"Z:\Quality\# # Document_Development\# Documents_for_Review\12000-00-00 Tables 9-11 Template OLD - TEST.doc" & vbNewLine & _
sForm & vbNewLine
With iMsg
Set .Configuration = iConf
.To = sFirstReviewer & "#indicate1.com"
.CC = "" 'sUserName & "; " & "johanson111#comcast.net"
.BCC = ""
.From = sUserName
.Subject = "Test Message"
.textbody = strbody
.HtmlBody = "Google Page"
.AddAttachment "Z:\Quality\# # Document_Development\12001-02-01 Document Review Form.pdf"
.AddAttachment "Z:\Quality\# # Document_Development\12001-02 Document Review Draft 9.doc"
.Send
End With
Debug.Print "CC = " & sUserName ' *******************************************
End Sub

Send email to multiple recipients (with recipients from textbox.value)

I am trying to create a taskmangement in Outlook with Custom Forms.
The custom form has a button (sendtask) a field assignto and a hidden textbox that copies the value from assignto to add to variable (myvalue) to include in To.
A user can assign a task with the assignto field. But with this way I can't send an email to multiple recipients.
Sub sendtask_click()
Set objPage = Item.GetInspector.ModifiedFormPages("Assign Task")
Set objControl = objPage.Controls("TextBox1")
Set oMsg = Application.CreateItem(olMailItem)
Set objNS = oMsg.Session
MyValue= objControl.Text
With oMsg
.To(MyValue)
.Subject = "New Task Assign"
.HTMLBody = "<HTML><BODY>You have new Task assign by " & _
objNS.CurrentUser.Name & " <h1>" & Item.Subject & _
"</h1><br/>" & "Description <br/>" & Item.Body & _
"<br/><br/>" & "Start Date:" & Item.StartDate & _
"<br/>" & "Due Date:" & Item.DueDate & "</BODY></HTML>"
.Send
End With
End Sub
How can I send an email to multiple recipients?
Did you try
With oMsg
.To = Join(Array("someone#here.com", MyValue),"; ")
'... rest of your with block code
End With