Thursday, June 02, 2005

Controlling Outlook from Excel

I needed to be able to email a sheet or a workbook to Outlook after
running some code on the sheet to strip the code, formulas etc. The
built in Excel Send To dialog just send the workbook or sheet as is.
I have attached the code for controlling outlook. all you need to do is
manipulate your workbook and set the wbname variable to be your
Workbook name.

To send one sheet only, copy your sheet to another
workbook first and delete the default sheets.







Sub MailToOutlook()
'notes:
'You need a reference to the Outlook library by
'going to Tools > References and finding
'the outlook library
'if you open this with a computer with outlook 2003
', the reference will change
'to Outlook 11 library, compared to Outlook 2000
'which will refer an outlook 9 library


wbname = ThisWorkbook.FullName

Dim objol As New Outlook.Application
Dim objmail As MailItem
Set objol = New Outlook.Application
Set objmail = objol.createitem(olmailitem)

With objmail
'enter email address
.To = "andrew@excelthoughts.com"
'.cc = "whoever" 'enter e email address
.Subject = "Email"
.Body = "Please find the attached file"
.NoAging = True
'adds attachment to email
.Attachments.Add wbname
.display
End With

'destroy objects
Set objmail = Nothing
Set objol = Nothing

End Sub


Labels: