How to open an email with field values from a form

Posted on 2014-03-11
Last Modified: 2014-03-11
Is it possible to open a New email (To: can be blank) via a form button, with selected control values appearing in the body of the email?  If so, please provide sample VBA code to do so.
Question by:David_W_R
  • 4
  • 2
LVL 57

Accepted Solution

Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 39920761
Here's one example:

Sub SendMessage(strTo As String, _
                               strSubject As String, strBody As String, strCC As String, _
                               Optional AttachmentPath)

    Dim objOutlook As Outlook.Application
    Dim objOutlookMsg As Outlook.MailItem

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

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

    With objOutlookMsg
        ' Add the To recipient(s) to the message.
        .To_ = strTo
        .CC = strCC
        .BodyFormat = olFormatHTML
        .HTMLBody = strBody
        .Subject = strSubject

        ' Add attachments to the message, multiple attachments separated with ;
        If Not IsMissing(AttachmentPath) Then
            .Attachments.Add (AttachmentPath)
        End If


    End With

    Set objOutlookMsg = Nothing
    Set objOutlook = Nothing

End Sub
LVL 57
ID: 39920766
Note BTW that this is an early bound example, so you need a reference set to Outlook.

You'd call this from a onclick event, passing the control value to the procedure.

Also note there are other ways to send mail, but you would need to provide the interface and then send the mail directly.


Author Comment

ID: 39920794
Just what I need if you can advise me as to how to define a data type?  e.g. Outlook.Application   (error: user-defined type not defined)
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

LVL 57
ID: 39920839
<<Just what I need if you can advise me as to how to define a data type?  e.g. Outlook.Application   (error: user-defined type not defined) >>

 You need to set a reference to Outlook in the VBA editor under tools/references.

LVL 57
ID: 39920848
Or you can go late bound:

 Dim objOutlook as Object

 Set objOutlook = CreateObject("Outlook.Application")

and forgo the reference.


Author Closing Comment

ID: 39920869
I'm on my way.   Many thanks!

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question