Outlook from Access

W

WebTracer

Developing an Access application and I want to send an email when the
user clicks a button. Outlook either generates errors (see code
comments) or says it has created mail, but it doesn't do anything.
Ideas?

Public Function SendMessageOutlook(strTo As String, strCC As String,
strSubject As String, strMsg As String) As Integer

'*******************
' The following function requires references be registered for:
' 1. Microsoft Outlook x.0 Object Library
' 2. And if Word is used as the Outlook email editor, Microsft Word
x.0 Object Library
'*******************

On Error GoTo Err_SendMessageOutlook

Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

'***Create the Outlook session.

Set objOutlook = CreateObject("Outlook.Application")

'***Create the message.

Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg

'This code block throws an error
'***Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(strTo)
objOutlookRecip.Type = olTo 'This should never be allowed to be
blank

'***Add the CC recipient(s) to the message.
'Set objOutlookRecip = .Recipients.Add(strCC)
'objOutlookRecip.Type = olCC 'If CC is blank it errors, it
might error if BCC is blank

'***Set the Subject, Body, and Importance of the message.
..Subject = strSubject
..Body = strMsg
..Importance = olImportanceHigh 'High importance

' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
If Not objOutlookRecip.Resolve Then
objOutlookMsg.Display
End If
Next
'.Send

End With

Set objOutlookMsg = Nothing
Set objOutlook = Nothing

SendMessageOutlook = 1

Exit Function

Err_SendMessageOutlook:
errormsg = "Error # " & Str(Err.Number) & " was generated by " &
Err.Source & Chr(13) & Err.Description
MsgBox errormsg, , "Error", Err.HelpFile, Err.HelpContext

SendMessageOutlook = 0

End Function

****Note - I DID NOT WRITE THIS - The original author of the software
did
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top