Microsoft Office Tutorials and References
In Depth Information
Composing an E‑mail in Outlook from Excel
The following macro first checks to see if Outlook is already open, and if so, Outlook is activated. If
Outlook happens to be closed, an Outlook application is created using the late binding method.
Sub OpenOutlook()
‘Declare and establish the Object variables for Outlook.
Dim objOutlook As Object
Dim objNameSpace As Object
Dim objInbox As Object
Set objOutlook = CreateObject(“Outlook.Application”)
Set objNameSpace = objOutlook.GetNamespace(“MAPI”)
Set objInbox = objNameSpace.Folders(1)
‘Activate Outlook if it is already open, or display Outlook’s
‘application object interface if it is closed.
On Error Resume Next
AppActivate (“Outlook”)
If Err.Number <> 0 Then objInbox.Display
End Sub
The CreateObject method provides an optional second argument for applications
residing on a server, for example: Set objOutlook = CreateObject(“Outlook
.Application”, “”) .
coMposing An E-MAil in ouTlook froM ExcEl
Most of the time, when you open Excel, whether manually or with VBA, it’s for the purpose of
doing something, such as to receive or send e-mails, but also to update your calendar or manage
your task list. Building upon the previous code that opens or activates Outlook, this section explains
how to compose and send a complete e-mail message.
creating a Mailitem object
Where VBA is concerned, MailItem is an Outlook object that you know (no doubt all too well) as
a typical e-mail message that arrives in your Inbox. The MailItem object is made up of the familiar
fields “To,” “CC,” and “Subject.” The other components of the MailItem object are the “Body”
where you type the text of your message; an optional level of Importance; and maybe an attachment.
When you want to compose an e-mail with VBA, you declare a variable for the MailItem object and
set it as a created item of the Outlook application object. For example, the following macro would
create an e-mail message:
Sub SendEmail()
‘Declare and establish the Object variables for Outlook.
Dim objOutlook As Object
Dim objNameSpace As Object
Search JabSto ::

Custom Search