Solved

How to open an email with field values from a form

Posted on 2014-03-11
6
335 Views
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.
0
Comment
Question by:David_W_R
  • 4
  • 2
6 Comments
 
LVL 57

Accepted Solution

by:
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

        .Display

    End With

    Set objOutlookMsg = Nothing
    Set objOutlook = Nothing

End Sub
0
 
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.

Jim.
0
 

Author Comment

by:David_W_R
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)
0
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.

 
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.

Jim.
0
 
LVL 57
ID: 39920848
Or you can go late bound:

 Dim objOutlook as Object

 Set objOutlook = CreateObject("Outlook.Application")

and forgo the reference.

Jim.
0
 

Author Closing Comment

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

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now