Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 356
  • Last Modified:

How to open an email with field values from a form

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
David_W_R
Asked:
David_W_R
  • 4
  • 2
1 Solution
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
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
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
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
 
David_W_RAuthor Commented:
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
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
<<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
 
Jim Dettman (Microsoft MVP/ EE MVE)PresidentCommented:
Or you can go late bound:

 Dim objOutlook as Object

 Set objOutlook = CreateObject("Outlook.Application")

and forgo the reference.

Jim.
0
 
David_W_RAuthor Commented:
I'm on my way.   Many thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now