[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to create an Outlook email referencing form controls

Posted on 2014-12-11
3
Medium Priority
?
351 Views
Last Modified: 2014-12-12
The following procedure worked in 2007, but gives compile error "User defined type not defined" in 2010 on the line in bold type below:

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

'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.
'You need to set a reference to Outlook in the VBA editor under tools/references.

'Or you can go late bound:
'Dim objOutlook As Object
'Set objOutlook = CreateObject("Outlook.Application")
'and forgo the reference.

    Dim objOutlook As Outlook.Application       'Error "User-defined type not defined"
    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
Comment
Question by:David_W_R
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 26

Accepted Solution

by:
Nick67 earned 1600 total points
ID: 40494544
'You need to set a reference to Outlook in the VBA editor under tools/references.
The old version clearly had the reference set, the new one does not

In the code editor, under Tools | References set a reference to Microsoft Outlook 14.0

And the way Outlook is opened is problematic.  The best way to do that is referenced here
http://www.experts-exchange.com/Programming/Microsoft_Development/A_17466-Properly-open-Outlook-as-an-Application-object-in-VBA.html
0
 
LVL 85

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 400 total points
ID: 40495127
but gives compile error "User defined type not defined" in 2010
You can use Late Binding, as suggested in your code and by Nick, but you can also use Early Binding if you have a good bit of control over your environment (and your end user environments). For example, if you know all users will be running AT LEAST Office 2007, then create your final deployment on a machine running 2007, and deploy it. Users running 2007 or later will "make" the reference to their installed version. Access has no trouble upsizing references, but it cannot downsize them.
0
 

Author Closing Comment

by:David_W_R
ID: 40496448
Thanks for the timely and effective help:  Nick67 with the instant fix, and Scott McDaniel with the pertinent future reference.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
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.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

650 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