Solved

How to create an Outlook email referencing form controls

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Use a Combo-Box to do a Search 26 23
Normalization of a table 19 69
Allow user to edit an outgoing email 2 23
VBA SQL statement - 2 "OR"s and 1 "And" 4 25
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
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…
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…
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…

813 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

8 Experts available now in Live!

Get 1:1 Help Now