VBA to include value of control in body of email (1 Viewer)

JPR

Registered User.
Local time
Today, 04:43
Joined
Jan 23, 2009
Messages
192
Hello,
I have found a code that opens Outlook, adds the email address in the To line and a text in the subject line.
I would like to place this code behind a button on a form. I was wandering if there is a way to include the value of certain controls on the form (ex. txtFname, txtLName) in the body of the email. This is the code:

" Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient

' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")

' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg
' Set the Subject, Body, and Importance of the message.
.Subject = "Test"
.Body = "Hello."
.To = "xxx@yahoo.com; xxx@gmail.com"
.Display
End With
Set objOutlook = Nothing
End Sub "

Thank you for your help.
 

Minty

AWF VIP
Local time
Today, 11:43
Joined
Jul 26, 2013
Messages
10,354
Yes - I would build up a string of the body text you want then refer to that in the outlook segment. Something like

Code:
Dim strBody as String

strBody = "Hello " & me.txtFName & " " & Me.txtLName & vbcrlf & vbcrlf
strBody = strBody & "Here is your email!"

Then in your outlook code

.Body = strBody

You can use the same idea and add HTML tags to format it in HTML if you want to make it pretty, but it is a little more involved using <b> etc.
 

JPR

Registered User.
Local time
Today, 04:43
Joined
Jan 23, 2009
Messages
192
Works great. Thank you
 

Minty

AWF VIP
Local time
Today, 11:43
Joined
Jul 26, 2013
Messages
10,354
You are welcome.
Good luck with the rest of your project.
 

Users who are viewing this thread

Top Bottom