Solved

How to create an Outlook email referencing form controls

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

734 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