Sending a Message

     

Besides simply reading messages, Outlook VBA also enables you to send messages. As you'll see over the next few sections, Outlook VBA gives you a number of ways to go about this.

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:

  MailItem  .Recipients.Add(  Name  ) 

MailItem

The MailItem object to which you want to add the recipient.

Name

The recipient's email address. If the recipient is in the Contacts list, you can just use his or her display name.

You can run the Add method as many times as you like for the same MailItem. Outlook separates each new recipient with a semicolon (;).

Each recipient in a message is a Recipient object and has the following properties (among others) :

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("bob@weave.com").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

  MailItem  .Send 

MailItem

The MailItem object you want to send.

graphics/note_icon.gif

If you add a recipient and then later decide to remove that person, use the Recipient .Delete method, which deletes the specified Recipient .


Listing 11.4 shows a procedure that creates a new MailItem object, sets up the recipient, subject, and body, and then sends the message.

Listing 11.4. A Procedure That Sends an Email 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 "blah@yadda.com"         .Subject = "Just Testing"         .Body = "This is only a test"         .Send     End With End Sub 


Absolute Beginner's Guide to VBA
Absolute Beginners Guide to VBA
ISBN: 0789730766
EAN: 2147483647
Year: 2003
Pages: 146

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net