Outlook Automation Part 4 - MailItem Method - finally arrives.
Part 4 – Looking at the MailItem Methods and Events
In this part we will investigate the Methods and Events of the mail item object.
So let’s dive straight in and look at what Methods we have available to us:
|
Method |
Description |
|
ClearConversationIndex |
Clears the index of the conversation thread for the mail message or post. |
|
Close (*) |
Close the MailItem, also note that there is an Event for this method. |
|
Copy |
Allows you to copy the Mail item from one folder to another |
|
Delete |
Delete the MailItem |
|
Display |
Calling this method will show the Outlook Mail Item dialog |
|
Forward (*) |
Executes the Forward Action for a Mail Item |
|
Move |
Allows you to move the mail item from one folder to another |
|
PrintOut |
The PrintOut method is the only Outlook method that can be used for printing. |
|
Reply |
Creates a reply, pre-addressed to the original sender, from the original message. Returns the reply as a MailItem object |
|
ReplyAll |
Creates a reply to all original recipients from the original message. Returns the reply as a MailItem object. |
|
Save |
Saves the Microsoft Outlook item to the current folder or, if this is a new item, to the Outlook default folder for the item type. |
|
SaveAs |
Saves the Microsoft Outlook item to the specified path and in the format of the specified file type. If the file type is not specified, the MSG format (.msg) is used. |
|
Send (*) |
Send the mail item |
|
ShowCategoriesDialog |
Displays the Show Categories dialog box, which allows you to select categories that correspond to the subject of the item |
Method: ClearConversationIndex
Not too sure what exactly this does so if anybody could shed some light on this, this would be helpful, thanks.
Example:
loMailItem.ClearConversationIndex()
Method: Close
Close the current item; pass the appropriate parameter to either ask for save, or just discard without prompting, also you can save without prompting as well.
Call: Close(SaveMode)
Where SaveMode = 0 – olSave = ‘Save without prompting’
1 – olDiscard = ‘Discard all changes without prompting’
2 – olPromptForSave = ‘Prompt to Save or discard changes’
Example:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loFold AS Outlook.Folders
LOCAL loMail AS Outlook.MailItem
#DEFINE olDiscard 1
#DEFINE olPromptForSave 2
#DEFINE olSave 0
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loMail = loOut.CreateItem(0) && MailItem
loMail.Subject = "Test For Close Method"
loMail.Display()
loMail.Close(olPromptForSave)
Method: Copy
Copy the current item to a new object.
The example code shows a few of the methods in action, it shows how to use the copy method and the move method; also we call the save to make sure this is saved into the new folder.
This example also shows you how to create a new mail folder in outlook using the Folders Collection.
Example:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loFold AS Outlook.MAPIFolder
LOCAL loNew AS Outlook.MAPIFolder
LOCAL loItem AS Outlook.MailItem
LOCAL loCopy AS Outlook.MailItem
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loFold = loNS.GetDefaultFolder(6) && Inbox
loNew = loFold.Folders.Add("Saved Mail",16) && olFolderDrafts
loItem = loOut.CreateItem(0) && New Mail Item
loItem.Subject = "Test for Copy into new folder."
loCopy = loItem.Copy()
loCopy.Move(loNew) && Copy this into the New Folder we created.
loCopy.Save
Method: Delete
Deletes the current selected object, this could be a mailitem, folder etc.
This example assumes you ran the Copy example first and did not manually remove these items from outlook.
Example:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loFold AS Outlook.MAPIFolder
LOCAL loNew AS Outlook.MAPIFolder
LOCAL loItem AS Outlook.MailItem
loFold = loNS.GetDefaultFolder(6) && New Folder is under Inbox.
loNew = loFold.Folders("Saved Mail") && Got Reference to New Folder.
loItem = loNew.Items(1) && Got the single Mail Item.
lcMsg = "Delete Message with subject: " + loItem.Subject
IF MESSAGEBOX(lcMsg,36+256,"Delete Mail Message") = 6
*-- In Outlook 2003, need to have a '=' sign otherwise
*-- you will get an error saying parameter is not optional.
=loItem.Delete
*-- Notice the same method used to remove the folder as well.
*-- Clean Up - Remove the New Folder.
=loNew.Delete
ENDIF
Method: Display
Parameters: Optional – make window modal
This method allows you display a selected object; this can be a mailitem, contactitem, etc.
Example: Show a Mail Item
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loFold AS Outlook.MAPIFolder
LOCAL loItem AS Outlook.MailItem
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loFold = loNS.GetDefaultFolder(6) && Inbox
loItem = loFold.Items(1) && Get the first Item
=loItem.Display(.T.) && Display as Modal Dialog
Example: Show the Deleted Items folder
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loFold AS Outlook.MAPIFolder
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loFold = loNS.GetDefaultFolder(3) && Deleted Items
loFold.Display && Display Outlook with this folder selected.
Method: Forward
This method executes a Forward action for the item.
Example:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loFold AS Outlook.MAPIFolder
LOCAL loItem AS OUTLOOK.MailItem
LOCAL loForw AS OUTLOOK.MailItem
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loFold = loNS.GetDefaultFolder(6) && InBox
loItem = loFold.Items(1) && Pick First Item
loForw = loItem.Forward && Create a new Mailitem from selected.
WITH loForw
.Recipients.Add("Fred Bloggs") && Will Cause Outlook Security Prompt.
.Display
ENDWITH
Method: Move
This method allows you to move items to new folders.
This example will use the Find and FindNext methods to find all message sent by ‘Dan Wilson’ and uses the Move method to move all e-mail messages that where sent by ‘Dan’ to a new folder.
Example:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loFold AS Outlook.MAPIFolder
LOCAL loItems AS Outlook.Items
LOCAL loItem AS Outlook.MailItem
LOCAL loNew AS Outlook.MAPIFolder
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loFold = loNS.GetDefaultFolder(6) && InBox
*-- Create a new folder for this example
loNew = loFold.Folders.Add("Dan Wilson Mail",6) && Inbox type.
*-- if folder already existed we would use the following code
*-- loNew = loFold.Folders("Dan Wilson Mail")
loItems = loFold.Items
loItem = loItems.Find("[SenderName] = 'Dan Wilson'")
DO WHILE VARTYPE(loItem) = 'O' AND !ISNULL(loItem)
=loItem.Move(loNew)
loItem = loItems.FindNext
ENDDO
Method: PrintOut
This method prints the outlook item using the default settings.
Example:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loFold AS Outlook.MAPIFolder
LOCAL loItem AS Outlook.MailItem
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loFold = loNS.GetDefaultFolder(6) && InBox
loItem = loFold.Items(1) && Get First Mail Item
=loItem.PrintOut
Method: Reply
This method creates a reply, pre-address to the original sender, from the original message, the object returned is a MailItem object.
Example:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loFold AS Outlook.MAPIFolder
LOCAL loItem AS Outlook.MailItem
LOCAL loRepl AS Outlook.MailItem
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loFold = loNS.GetDefaultFolder(6) && InBox
loItem = loFold.Items(1) && Get First Mail Item
loRepl = loItem.Reply && Security Dialog Appears.
loRepl.Body = "Replying to this " + CHR(13)+CHR(10) + loRepl.Body
loRepl.Display
Method: ReplyAll
This method creates a reply to all original recipients from the original message, returns the reply as a MailItem object.
Example:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loFold AS Outlook.MAPIFolder
LOCAL loItem AS Outlook.MailItem
LOCAL loRepl AS Outlook.MailItem
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loFold = loNS.GetDefaultFolder(6) && InBox
loItem = loFold.Items(1) && Get First Mail Item
loRepl = loItem.ReplyAll && Security Dialog Appears.
loRepl.Body = "Replying to this " + CHR(13)+CHR(10) + loRepl.Body
loRepl.Display
Method: Save
This method saves the Outlook item to the current folder or, if this is a new item, to the Outlook default folder for the item type.
Example:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loItem AS Outlook.MailItem
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loItem = loOut.CreateItem(0) && New Mail Item
WITH loItem AS Outlook.MailItem
.Subject = "Test for Save - should go to drafts folder"
.Recipients.Add("Fred Bloggs") && Security Dialog.
.Save
ENDWITH
Method: SaveAs
This method saves the Outlook item to the specified path and in the format of the specified file type, if file type is not specified then the MSG format (.msg) is used.
Parameters for this method are: Path, Type
Where Type: is one of the olSaveAsType constants.
|
Constant |
Value |
Description |
|
olTXT |
0 |
Text Format |
|
olRTF |
1 |
Rich Text Format - Need Word as Mail Editor |
|
olTemplate |
2 |
Outlook Template |
|
olMSG |
3 |
MSG Format – Default |
|
olDoc |
4 |
Word Format - Need Word as Mail Editor |
|
olHTML |
5 |
HTML Format |
|
olVCard |
6 |
VCard format – Contact Items not MailItems |
|
olVCal |
7 |
VCal format – Calendar Items |
|
olICal |
8 |
ICal format – Calendar Items |
|
olMSGUnicode |
9 |
MSG UniCode format. |
Taken from Outlook Help file
Remarks
When you use the SaveAs method to save items to the file system, you receive an “address book” warning message. This includes all types of items, whether or not the items have attachments or active content. This change has been made so that someone cannot programmatically save items to a file and then parse the file to retrieve e-mail address. Also note that even though ‘olDoc’ and ‘olRTF’ is a valid olSaveAsType constants, messages in HTML format cannot be saved in Document format, and the olDoc, olRTF constants work only if Microsoft Word is setup as the default email editor.
Example:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loFold AS OUTLOOK.MAPIFolder
LOCAL loItem AS Outlook.MailItem
LOCAL lcName AS String
LOCAL lcPrompt AS String
#DEFINE olTXT 0
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loFold = loNS.GetDefaultFolder(6) && Inbox
*-- Work Through the Inbox and save items
FOR EACH loItem IN loFold.Items
&& You may need to remove invalid filename characters
&& from the Subject before saving to file.
lcName = loItem.Subject
lcPrompt = "Are you sure you want to save the item? " + ;
"If a file with the same name already exists, " + ;
"it will be overwritten with this copy of the file."
IF MESSAGEBOX(lcPrompt,36+256,lcName) = 6
=loItem.SaveAs("C:\Temp\" + lcName + ".txt",olTXT)
ENDIF
ENDFOR
Method: Send
This method sends the appointment, meeting item, e-mail message or task item.
Example:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loItem AS Outlook.MailItem
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loItem = loOut.CreateItem(0) && Mail Item
loItem.Subject = "Test for Send"
loItem.Recipients.Add("Fred Bloggs") && First Security Dialog
loItem.Recipients.ResolveAll
loItem.Body = "This is a Test Email"
loItem.Send && Another Security Dialog - Need to Wait for timer.
Method: ShowCategoriesDialog
This method displays the Show Categories dialog box, which allows you to select categories that correspond to the subject of the item.
Exmaple:
LOCAL loOut AS Outlook.Application
LOCAL loNS AS Outlook.NameSpace
LOCAL loItem AS Outlook.MailItem
loOut = CREATEOBJECT('Outlook.Application')
loNS = loOut.GetNamespace("MAPI")
loItem = loOut.CreateItem(0) && Mail Item
loItem.Subject = "Test for Categories dialog."
loItem.Recipients.Add("Fred Bloggs") && Security Dialog
loItem.Display
loItem.ShowCategoriesDialog