Function EmailUsingOutlook(bDisplayMsg As Boolean, _
Optional strAttachmentPath) _
As Boolean
On Error GoTo ErrorHandling_Err
' ------------------------------------------------------------------
' Purpose: Example of how to send e-mail to MS Outlook
' with or without an attached file
'
' Accepts: bDisplayMsg and strAttachmentPath, which is optional
'
' Returns: True, if successful, otherwise False
'
' Example usage: bRetVal = EmailUsingOutlook(False,"C:\Directions.doc")
'
' NOTE: Reminder to reference the MS Outlook Object Library
' prior to calling this procedure.
' ------------------------------------------------------------------
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")
' Start the creation of the email message
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message
Set objOutlookRecip = .Recipients.Add("Anyone@Yahoo.com")
objOutlookRecip.Type = olTo '(NOTE: This can also be olBCC, olCC, or olOriginator)
' Set the subject and body of our new message
.Subject = "Lunch today, Mr. Smith?"
.Body = "Mr. Smith, can we discuss CodeDisk at lunch today?" & vbCrLf & vbCrLf
' Set the importance of our message
' (NOTE: This can be olImportanceLow, olImportanceNormal, or olImportanceHigh)
.Importance = olImportanceNormal
' Add any attachments to the message, if applicable,
' which is determined by the second argument of this function
If Not IsMissing(strAttachmentPath) Then
Set objOutlookAttach = .Attachments.Add(strAttachmentPath)
End If
' Next, check to see if the recipient is in the address book.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
' Next, check the first argument of this function to
' determine whether to display the email message before sending it
If bDisplayMsg Then
.Display
Else
.Send
End If
End With
' We're done so, destroy our Outlook object
Set objOutlook = Nothing
' Next, inform the calling function whether this function
' was successful or not
EmailUsingOutlook = (Err = 0)
ErrorHandling_Err:
If Err Then
'Trap your error(s) here, if any!
End If
End Function