Working with Email Messages

     

After you've got your NameSpace session and have referenced the MAPIFolder object you want to work with, you'll probably want to do something with the messages in that folder: display them, move them, respond to them, and so on. To access the messages, you use the MAPIFolder object's Items collection, which contains all the messages in the folder. Each of these messages is a MailItem object, and it's to this object that you'll turn your attention for most of the rest of this chapter.

MailItem Object Properties

The MailItem object boasts dozens of properties that cover everything from the message recipients to the assigned sensitivity. Here's a list of the most useful MailItem properties:

MailItem .BCC ? Returns the display names (separated by semicolons) of the addresses listed as blind courtesy copy recipients for the specified MailItem .

MailItem .Body ? Returns or sets the body text for the specified MailItem .

MailItem .BodyFormat ? Returns or sets the format of the body text for the specified MailItem . Possible values are the following constants: olFormatHTML , olFormatPlain , olFormatRichText , and olFormatUnspecified .

MailItem .CC ? Returns the display names (separated by semicolons) of the addresses listed as courtesy copy recipients for the specified MailItem .

MailItem .FlagDueBy ? Returns or sets the due date for a flagged MailItem . Note that the FlagStatus property (described below) must be set to olFlagMarked .

MailItem .FlagRequest ? Returns or sets a string that specifies the action to take for a flagged MailItem . Note that the FlagStatus property must be set to olFlagMarked .

MailItem .FlagStatus ? Returns or sets the flag status for the specified MailItem . To set a flag, use olFlagMarked ; to set a flag's status to "complete," use olFlagComplete ; to remove a flag, use olNoFlag .

MailItem .HTMLBody ? Returns or sets the HTML body text for the specified MailItem .

MailItem .Importance ? Returns or sets the importance level for the specified MailItem . This property can be one of the following constants?TT>olImportanceHigh, olImportanceLow , or olImportanceNormal .

MailItem .ReadReceiptRequested ? Returns True if the sender has requested a read receipt for the specified MailItem ; returns False otherwise .

MailItem .ReceivedTime ? Returns or sets the date and time when the specified MailItem was received.

MailItem .Recipients ? Returns a Recipients object ”the collection of recipients ”for the specified MailItem . See "Specifying the Message Recipients," later in this chapter.

MailItem .SenderName ? Returns the display name of the sender of the specified MailItem .

MailItem .SenderEmailAddress ? Returns the email address of the sender of the specified MailItem .

MailItem .Sensitivity ? Returns or sets the sensitivity level of the specified MailItem . This property can be one of the following constants?TT>olConfidential, olNormal , olPersonal , or olPrivate .

MailItem .SentOn ? Returns the date and time when the specified MailItem was sent.

MailItem . Size ? Returns the size of the specified MailItem in bytes.

MailItem .Subject ? Returns or sets the subject line of the specified MailItem .

MailItem .To ? Returns the display names (separated by semicolons) of the addresses listed in the To line of the specified MailItem . To learn how to add recipients, see "Specifying the Message Recipients," later in this chapter.

MailItem .UnRead ? Returns True if the specified MailItem has not been read; returns False otherwise. You can also set this property.

MailItem Object Methods

With the methods available to the MailItem object, you can send messages, as well as reply to and forward messages. See "Sending a Message," later in this chapter, to learn how to use these methods that send messages. Otherwise, you can also open messages, move them to another folder, delete them, and more. Here's a summary of some of these more useful MailItem object methods:

MailItem .Close ? Closes the window in which the specified MailItem object is displayed (see the Display method, later in this list). This method uses the following syntax:

  MailItem  .Close(  SaveMode  ) 

MailItem

The MailItem object you want to close.

SaveMode

A constant that determines how the window is closed:

 

olDiscard

Closes the window without saving changes.

 

olPromptForSave

Prompts the user to save changes.

 

olSave

Saves changes automatically.

MailItem .Copy ? Creates a copy of the specified MailItem object. This method returns a MailItem object that represents the copy.

MailItem .Delete ? Deletes the specified MailItem object (that is, sends the message to the Deleted Items folder).

MailItem .Display ? Displays the specified MailItem object in a new window using the following syntax:

  MailItem  .Display(  Modal  ) 

MailItem

The MailItem object you want to work with.

Modal

(optional) Use True to display the message in a modal window, which means the user can't switch back to Outlook until he or she closes the window; use False for a nonmodal window (this is the default).

MailItem .Move ? Moves the specified MailItem object to a different folder using the following syntax:

  MailItem  .Move(DestinationFolder) 

MailItem

The MailItem object you want to work with.

DestinationFolder

The MAPIFolder object to which you want to move the message.

MailItem .PrintOut ? Prints the specified MailItem object.

MailItem .Save ? Saves the specified MailItem object.

Listing 11.3 shows a procedure that runs through the messages in the Inbox folder.

Listing 11.3. A Procedure That Processes Inbox Messages
 Sub ProcessInboxMessages()     Dim ns As NameSpace     Dim ib As MAPIFolder     Dim msg As MailItem     '     ' Set up the namespace     '     Set ns = ThisOutlookSession.Session     '     ' Get the default Inbox folder     '     Set ib = ns.GetDefaultFolder(olFolderInbox)     '     ' Run through each item in the Inbox     '     For Each msg In ib.Items         '         ' Flag important messages         '         If msg.Importance = olImportanceHigh Then             msg.FlagStatus = olFlagMarked             msg.FlagRequest = "Handle this, will ya!"             msg.FlagDueBy = Date + 7             msg.Importance = olImportanceNormal             msg.Save         End If         '         ' Look for expired flags         '         If msg.FlagDueBy < Date Then             msg.Display             MsgBox "The displayed message has an expired flag!"         End If         '         ' Move sensitive messages to "Confidential" folder         '         If msg.Sensitivity = olConfidential Then             msg.Move ns.Folders(1).Folders("Confidential")         End If     Next 'msg End Sub 

This procedure loops through the messages in the Inbox folder. It checks for three things:

  • If a message was sent with "high" importance, it's marked with a flag, and the due date is set to a week from now.

  • If a message has an expired flag, the message is displayed.

  • If a message was sent with "confidential" sensitivity, it's moved to a folder named "Confidential." (If you plan on trying out this code, make sure this folder exists. )



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