|
The MailItem ObjectThe MailItem object represents a message in a mail folder. This section also covers some of the methods and properties of the PostItem object. Properties of the PostItem object are similar to the properties of the MailItem object, so they are discussed together. Open the MailItem object and PostItem object items in the VBScript Samples folder to work directly with this code in Outlook.
MailItem Object MethodsThis section covers the Send, Reply, and Close methods of the MailItem object. If your code attempts to send a message programmatically, the Warning dialog box shown in Figure 11-9 will be displayed. See Chapter 13 for methods to eliminate the Warning dialog box. Figure 11.9 - This Warning dialog box appears when you attempt to send a message programatically. Sending a MessageYou can use the Send method of the MailItem object to send a message to a recipient. The following example creates a Mail Message item, sets the Subject and message Body fields, and then sets the To field to your name. You can retrieve the ScriptText property of the FormDescription object for the item to set the Body property of the message. It then uses the Send method to send the item to the specified recipient. The Body property of the item only lets you set plain text as the message text. If you want to format the Body programmatically, use the HTMLBody property discussed later in this section.
Sub SendAMessage_Click
Set MyItem = Application.CreateItem(olMailItem)
MyItem.Subject = "VBScript Code for MailItem object"
Set MyForm = Item.FormDescription
MyItem.Body = MyForm.ScriptText
MyItem.To = Application.GetNameSpace("MAPI").CurrentUser
MyItem.Send
End Sub
Replying to a MessageYou can use the Reply method of a MailItem object to return a Reply item. The following example creates a Reply item based on the current item, and then returns the Reply item represented by the MyReply object. The Reply item is then displayed. To run this example, you must first put a CommandButton1 control on the Read page of a Mail Message form. Exit Design mode and send the item to your Inbox, and then open the item in the Inbox and click CommandButton1. The Reply item is displayed.
Sub CommandButton1_Click
Set MyFolder = Application.GetNameSpace("MAPI").GetDefaultFolder(6)
Set MyItem = MyFolder.Items.Item(1)
Set MyReply = MyItem.Reply
MyReply.Display
End Sub
Closing an ItemYou can use the Close method of the MailItem object to close an item. When you close the item, you also close the item’s associated form. The following example closes the current item when the CloseAnItem button is clicked. Note that if the blnIsDirty script-level variable is True, then the user is prompted to save changes. If blnIsDirty is False, then the item is closed and changes are not saved. You could modify this procedure to automatically save changes to a dirty form. This example also illustrates how you can write event procedures in the code for your custom form to control how and when the form is saved.
Dim blnIsDirty
Sub Item_PropertyChange(ByVal Name)
blnIsDirty = True
End Sub
Sub Item_CustomPropertyChange(ByVal Name)
blnIsDirty = True
End Sub
Function Item_Write()
blnIsDirty = False
End Function
Sub CloseAnItem_Click
Const olSave = 0
Const olDiscard = 1
Const olPromptForSave = 2
If blnIsDirty = True Then
Item.Close olPromptForSave
Else
Item.Close olDiscard
End If
End Sub
You can use one of the arguments in the table with the Close method or use CONST declarations as shown in the preceding example.
Unlike previous versions of Outlook, Outlook 2000 and 2002 allow you to write VBA or Visual Basic code to respond to all the events that are raised in a Message item. For additional information, see Chapter 9 and Chapter 14.
MailItem and PostItem Objects PropertiesThis section covers the GetInspector, Body, HTMLBody, To, and SenderName properties of the MailItem and PostItem objects. Using GetInspector To Reference the FormYou can use the GetInspector property of the MailItem object or the PostItem object to reference the form associated with an item. You can then reference the page on the form, and then the controls on the page. The following example uses the GetInspector property of the MailItem object to return the form associated with the item. It references the Message page on the form and then sets the Visible property of the TextBox1 control to False. Before you run this example, do the following:
Sub CommandButton1_Click
Set MyPage = GetInspector.ModifiedFormPages("Message")
MyPage.TextBox1.Visible = False
End Sub
Setting the Message Text of an ItemYou can use the Body property of a MailItem object or a PostItem object to specify the text that appears in the Message control of a Mail Message. The following example creates a Reply item, enters text in the Message control of the Reply item, and then sends the form:
Sub CommandButton1_Click()
Dim MyFolder, MyItem, MyReply
Set MyFolder = Application.GetNamespace("MAPI").GetDefaultFolder(6)
Set MyItem = MyFolder.Items(1)
Set MyReply = MyItem.Reply
MyReply.Body = "Let’s go to the Napa Valley next weekend."
MyReply.Display
End Sub
The Body property does not support Rich Text Format (RTF) or HyperText Markup Language (HTML).
You can also use the HTMLBody property of a MailItem or PostItem object to specify formatted text in the Message control. When you set this property, Outlook automatically sets the EditorType property of the associated Inspector object to olEditorHTML(2). The following example displays the current editor type, sets the HTMLBody property of the item, and then displays the new editor type. Open the HTMLBody Object form in the VBScript Samples folder to see a more sophisticated example of using the HTMLBody property. You can create HTML dynamically in the VBScript code behind a form and then present that HTML in the body of an Item.
Sub CommandButton1_Click
On Error Resume Next
strHTML= "<body bgcolor=‘cyan’>" _
& "<H1>This is HTML text.</H1>" _
& "<body>"
Item.shtmlLBody = strHTML
End Sub
For additional information on the Body property, see Knowledge Base article Q291153-OL2002: Working With the Message or Body of an Outlook Item.
Setting the To Field of an ItemYou can use the To property of the MailItem object to set the value of a To field. The following example creates a new item and then sets the To field and Subject field values of the item:
Sub CommandButton1_Click
Set MyItem = Application.CreateItem(0)
MyItem.To = "someone@somecompany.com"
MyItem.Subject = "How to set the To field"
MyItem.Display
End Sub
Getting the Sender Name of an ItemYou can use the SenderName property of the MailItem object to return the name of the person who sent the message. The following example gets the first item in the Inbox and sets the Recip variable to the value of the SenderName property. It then creates a new Message item and sets its To field to the value of the Recip variable. When the item is displayed, the value of the SenderName property shows in the To box of the form.
Sub GetTheSenderName_Click
Set MyFolder = Application.GetNameSpace("MAPI").GetDefaultFolder(6)
Set MyItem = MyFolder.Items(1)
Recip = MyItem.SenderName
Set MyNewItem = Application.CreateItem(0)
MyNewItem.To = Recip
MyNewItem.Display
End Sub
In the preceding example, the name in the To field is not resolved. To resolve a name, the name must be added to the Recipients collection object. For more information, see "The Recipients Collection Object" later in this chapter.
Getting the Sender Address of an ItemThe Outlook Object Model does not provide a means to obtain the e-mail address of the item’s sender. To obtain the e-mail address of the sender, you can use CDO to create a Sender object for the MAPI Message object. The Sender object exposes several useful properties, one of which is the Address property. The following VBA code displays a message box containing the e-mail address for the sender of the first item in the Inbox:
Sub GetSender()
Dim objCDO As MAPI.Session
Dim objMsg As MAPI.Message
Dim objSender As MAPI.AddressEntry
Dim oMsg As Outlook.MailItem
Dim strMsg As String
On Error Resume Next
Set oMsg = Application.ActiveExplorer.Selection.Item(1)
Set objCDO = CreateObject("MAPI.Session")
objCDO.Logon "", "", False, False
Set objMsg = objCDO.GetMessage(oMsg.EntryID)
Set objSender = objMsg.Sender
strMsg = "Subject: " & objMsg.Subject & vbCrLf _
& "Sender Name: " & objSender.Name & vbCrLf _
& "Sender e-mail: " & objSender.Address & vbCrLf _
& "Sender type: " & objSender.Type
MsgBox strMsg, vbInformation
End Sub
Adding Attachments to an ItemYou can add attachments to an item programmatically by using the Add method of the Attachments collection object. VBScript’s FileSystemObject lets you write and read from a file programmatically. Therefore, you can write to a file and then attach the file to a message in code. Open the FileSystemObject item in the VBScript Samples folder to see this code in action.
Sub WriteandAttachFile_Click()
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const olByValue = 1, olByReference = 4, olEmbeddeditem = 5, olOLE = 6
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
strPath = GetTempDir & "\FSObject.txt"
Set objFile = fso.OpenTextFile(strPath, ForAppending, True)
strLine = Now & " - " & "This file demonstrates use " _
& "of the FileSystemObject to write to a text file."
objFile.Write(strLine)
objFile.Close
Set objMsg = Application.CreateItem(0)
Set colAttachments = objMsg.Attachments
colAttachments.Add strPath, olByValue, 1, "FileSystemObject Attachment"
objMsg.Subject = "FileSystemObject Object"
objMsg.Display
End Sub
Function GetTempDir
Const TemporaryFolder = 2, SystemFolder = 1, WindowsFolder = 0
On Error Resume Next
Dim fso, tfolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set tfolder = fso.GetSpecialFolder(TemporaryFolder)
If Err then
GetTempDir = "Could not obtain temporary folder path."
Exit Function
End If
GetTempDir = lcase(tFolder.Path)
End Function
|