Public Function CreateEmailWithOutlook( _
MessageTo As String, _
MessageCC As String, _
Subject As String, _
MessageBody As String, _
Optional AttachmentFile As Variant = Null)
' Define app variable and get Outlook using the "New" keyword
Dim olApp As Object 'Outlook.Application
Dim olMailItm As Object 'Outlook.MailItem ' An Outlook Mail item
Dim olFolder As Object 'Outlook.Folder
Dim olNameSpace As Object 'Outlook.NameSpace
Dim varItem As Variant
'Set olApp = New Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Set olNameSpace = olApp.GetNamespace("MAPI")
Set olFolder = olNameSpace.GetDefaultFolder(olFolderInbox)
' Create a new email object
Set olMailItm = olFolder.Items.Add(olMailItem)
' Add the To/Subject/Body to the message and display the message
With olMailItm
.To = MessageTo
.CC = MessageCC
.Subject = Subject
.body = MessageBody
' request a readers reciept so I know the email has been read
.ReadReceiptRequested = True
If IsArray(AttachmentFile) Then
For Each varItem In AttachmentFile
.Attachments.Add varItem
Next
ElseIf IsNull(AttachmentFile) = False Then
.Attachments.Add AttachmentFile
End If
'.Display ' To show the email message to the user
.Importance = olImportanceHigh
.Send
End With
' Release all object variables
olApp.Quit
Set olMailItm = Nothing
Set olFolder = Nothing
Set olNameSpace = Nothing
Set olApp = Nothing
End Function