Public Function SendDocAsAttachment(ByVal strFile As String, _
ByVal strRecips As String) As Boolean
On Error GoTo ErrHandler
Dim ol As Outlook.Application
Dim msg As Outlook.MailItem
Dim fldOut As Outlook.MAPIFolder
Dim cbrMenu As Office.CommandBar
Dim cbrTools As Office.CommandBarPopup
Dim cbrSend As Office.CommandBarControl
Dim lngCount As Long
Dim blnOpen As Boolean
If dir(strFile) <> "" Then
On Error Resume Next
Set ol = GetObject(, "Outlook.Application")
If Err = 0 Then
blnOpen = True
Else
Set ol = CreateObject("Outlook.Application")
End If
On Error GoTo ErrHandler
Set msg = ol.CreateItem(olmailitem)
With msg
.To = strRecips
.Attachments.Add strFile
.Subject = "Please see attached file"
.Body = "Here's the latest report" & vbCrLf
.Send 'This just places mail in outbox
End With
[green]'if you want to send now, you have to
'manipulate the commandbars collection since
'Outlook doesn't expose a method to do it.
'open an explorer[/green]
Call ol.Session.GetDefaultFolder(olFolderOutbox).GetExplorer.ShowPane(olFolderList, False)
Set fldOut = ol.Session.GetDefaultFolder(olFolderOutbox)
lngCount = fldOut.Items.count
[green]'capture the send action menu[/green]
Set cbrMenu = ol.ActiveExplorer.CommandBars("Menu Bar")
Set cbrTools = cbrMenu.Controls("Tools")
Set cbrSend = cbrTools.Controls("Send")
[green]'Send now[/green]
cbrSend.Execute
If Not blnOpen Then
Do While fldOut.Items.count >= lngCount
DoEvents
Loop
End If
SendDocAsAttachment = True
End If
ExitHere:
On Error Resume Next
Set msg = Nothing
Set cbrSend = Nothing
Set cbrTools = Nothing
Set cbrMenu = Nothing
If Not blnOpen Then
ol.Quit
End If
Set fldOut = Nothing
Set ol = Nothing
Exit Function
ErrHandler:
MsgBox "Error (" & Err & ") - " & Err.Description
Resume ExitHere
End Function