Solved

How to create an Outlook email referencing form controls

Posted on 2014-12-11
3
317 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 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

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…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
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.

749 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