May 19, 2011, 7:19 a.m.
posted by dante
Sending a Message
Creating a New Message
To send a new message (that is, one that isn't a reply or forward), you first need to create a new MailItem object. You do this by invoking the Application object's CreateItem method and specifying the olMailItem constant as the type of item you want to create.
For example, the following statements declare a MailItem object and then create it:
Dim mi as MailItem Set mi = Application.CreateItem(olMailItem)
Creating a Reply or Forward
Alternatively, you can create a MailItem object by replying to or forwarding an existing message. You have three choices:
MailItem.Forward— Forwards the specified MailItem object. This method returns a new MailItem object that represents the message to be forwarded.
MailItem.Reply— Replies to the sender of the specified MailItem object. This method returns a new MailItem object that represents the reply to be sent.
MailItem.ReplyAll— Replies to the sender and to all the other recipients of the specified MailItem object. This method returns a new MailItem object that represents the reply to be sent.
Here's a code snippet that sets up a reply to the first message in the Inbox folder:
Dim ns As NameSpace Dim ib As MAPIFolder Dim msg As MailItem Dim msgReply As MailItem ' ' Set up the namespace ' Set ns = ThisOutlookSession.Session ' ' Get the default Inbox folder ' Set ib = ns.GetDefaultFolder(olFolderInbox) ' ' Get the first message ' Set msg = ib.Items(1) ' ' Create the Reply ' Set msgReply = msg. Reply
Specifying the Message Recipients
Now that your MailItem object has been created, you may also need to add one or more recipients. The collection of recipients for a MailItem object is contained in the Recipients object. To add a recipient, you use the Recipients object's Add method:
You can run the Add method as many times as you like for the same MailItem. Outlook separates each new recipient with a semicolon (;).
Recipient.Address— Returns or sets the email address of the specified Recipient.
Recipient.Name— Returns or set the display name of the specified Recipient.
Recipient.Type— Determines the address line to which the specified Recipient will be added (To, Cc, or Bcc). Use olTo for the To line, olCC for the Cc line, or olBCC for the Bcc line. For example, assuming that msg is an object variable that represents a MailItem, the following statements add two recipients—one on the To line and one on the Cc line:
msg.Recipients.Add("Millicent Peeved").Type = olTo msg.Recipients.Add("[email protected]").Type = olCC
Sending the Message
With the recipients determined, you can also tweak other MailItem properties such as Subject, Body, and Importance (see "MailItem Object Properties Object Properties," earlier in this chapter). With that done, you can then send the message by running the Send method
Listing 11.4 shows a procedure that creates a new MailItem object, sets up the recipient, subject, and body, and then sends the message.
Sub SendAMessage() Dim ns As NameSpace Dim msg As MailItem ' ' Set up the namespace ' Set ns = ThisOutlookSession.Session ' ' Create the new MailItem ' Set msg = Application.CreateItem(olMailItem) ' ' Specify the recipient, subject, and body ' and then send the message ' With msg ' ' Adjust the following address! ' .Recipients.Add "[email protected]" .Subject = "Just Testing" .Body = "This is only a test" .Send End With End Sub