How to create an Outlook email referencing form controls
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