Managing Items in Outlook

This chapter has described and compared the methods and properties related to creating or opening documents in Word, Excel, and PowerPoint. Chapters 7 and 8 describe integrated Office solutions involving the creation of documents and content based on data in an Access database. They also describe the creation of an Outlook mail message. Below is a procedure similar to the one used in Chapters 7 and 8. It's provided here in the context of creating types of documents in other Office applications.

In the Visual Basic Editor in Outlook, insert a new standard code module and copy the following procedure. You can see the similarities of the methods for saving, closing, and printing. Outlook uses the CreateItem method to create all item types supported in Outlook, such as appointment, mail, contact, and notes. In other Office applications, you use the Add method to create new documents in which content will reside.

Sub NewMailItem()
    Dim itemNewMail As Outlook.MailItem
    Set itemNewMail = Application.CreateItem(olMailItem)
    With itemNewMail
        .To = "Energy Committee"
        .CC = "Executive Board"
        .Subject = "New energy usage documents"
        .Body = "The following documents reflect" & _
            " the energy usage for the circuit." & _
            vbCrLf
        '.Attachments.Add sFile
        .Display
        .Save
        .SaveAs Path:="C:\Temp\" & .Subject & ".msg", _
            Type:=olMSG
        MsgBox "Item has been saved to the Drafts folder."
        .PrintOut
        .Close SaveMode:=olPromptForSave
    End With
End Sub

The Save method in Outlook saves the mail item in this procedure to the Drafts folder. The SaveAs method saves a copy of the mail item to the file system. The Path argument in the SaveAs method in Outlook is equivalent to the FileName argument in the SaveAs method in Word, Excel, and PowerPoint. In the procedure above, you use the subject as the filename of the saved message. This is similar to Outlook's functionality in which you open a mail message in Outlook, click Save As on the File menu, and then click Save in the SaveAs dialog box.

The file's default name is the subject of the mail message. The second argument of the SaveAs method is named Type and is equivalent to the FileFormat argument in the SaveAs method in Word, Excel, and PowerPoint. The Type argument can be one of the OlSaveAsType constants, including olDoc, olHTML, olMSG, olRTF, or olTXT. The constant olDoc saves the message as a Word document.

The PrintOut method here doesn't take any arguments, unlike the PrintOut method in Word, Excel, and PowerPoint. It simply prints out the Outlook item. The above procedure prints the mail message.

Finally, the Close method is similar to Word's and Excel's in that it provides an argument that tells the application whether the user should be prompted to save. In the procedure above, you set the SaveMode argument to olPromptForSave, to prompt the user if changes should be saved. However, setting this argument is redundant in this example because the mail item is already saved using the Save method. However, the SaveMode argument is useful when you create a skeleton mail message, allow the user to make changes to the item, and then close the message programmatically using the Close method.