Solved

How to create an Outlook email referencing form controls

Posted on 2014-12-11
3
306 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
3 Comments
 
LVL 26

Accepted Solution

by:
Nick67 earned 400 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 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 100 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
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…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

760 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

23 Experts available now in Live!

Get 1:1 Help Now