Outlook Automation Part 2 - Lets start looking at the Object Model
So how do we make Outlook Visible, like Word, Excel and the rest?
Outlook is the only one not to have a visible property.
So the way we make Outlook visible is with the following code:
LOCAL loOutlook AS Outlook.Application
LOCAL loNameSpace AS Outlook.NameSpace
LOCAL loExplorer AS Outlook.Explorer
#DEFINE olFolderDisplayNormal 0
loOutlook = CREATEOBJECT('Outlook.Application')
loNameSpace = loOutlook.GetNamespace("MAPI")
loExplorer = loOutlook.Explorers.Add(loNameSpace.Folders[1],olFolderDisplayNormal)
loExplorer.Activate
The above code should open Outlook with the Outlook Today page.
Also to let you know at this point, you can make the individual folder pages visible, so for example if I wanted the Contacts Page to be visible, I can do this with the following code:
LOCAL loOutlook AS Outlook.Application
LOCAL loNameSpace AS Outlook.NameSpace
LOCAL loContacts AS Object
#DEFINE olFolderContacts 10
loOutlook = CREATEOBJECT('Outlook.Application')
loNameSpace = loOutlook.GetNamespace("MAPI")
loContacts = loNameSpace.GetDefaultFolder(olFolderContacts)
loContacts.Display
One thing to take note of is, that if Outlook is already running you will get a new window with just the contacts page, yet if Outlook is not running you will get the main Outlook window with the contacts page open, so let’s move on.
How do we get object references to the folders?
You can do this in a couple of ways:
1. You can go via the NameSpace.Folders collection like so:
loCalendar = loNameSpace.Folders[1].Folders["Calendar"]
2. The best way is to use the NameSpace.GetDefaultFolder Method like so:
#DEFINE olFolderCalendar 9
loCalendar = loNameSpace.GetDefaultFolder( olFolderCalendar )
At this point I will give you the list of Folders and there appropriate value:
|
Constant |
Value |
Constant |
Value |
|
olFolderDeleted |
3 |
olFolderContacts |
10 |
|
olFolderOutbox |
4 |
olFolderJournal |
11 |
|
olFolderSent |
5 |
olFolderNotes |
12 |
|
olFolderInBox |
6 |
olFolderTasks |
13 |
|
olFolderCalendar |
9 |
olFolderDrafts |
16 |
New to Outlook 2003 was the Junk folder this Constant is ‘olFolderJunk’ Value = 23
So with these constants we can get the folders we need, so let’s say I wanted to find out how many messages and folders the Inbox contained, so the way to do this is like so:
LOCAL loOutlook AS Outlook.Application
LOCAL loNameSpace AS Outlook.NameSpace
LOCAL loInBox AS Object
#DEFINE olFolderInBox 6
loOutlook = CREATEOBJECT('Outlook.Application')
loNameSpace = loOutlook.GetNamespace("MAPI")
loInBox = loNameSpace.GetDefaultFolder(olFolderInBox)
*-- At this point we have the InBox Object.
*-- Let’s display how many messages we have in the Inbox.
? “We have “ + TRANSFORM(loInBox.Items.Count) + “ Messages”
*-- What about subfolders in the inbox.
? “The inbox contains “ + TRANSFORM(loInBox.Folders.Count) + “ SubFolders”
This next example obtains any Reminder Messages you have set using the Calendar.
LOCAL loOutlook AS Outlook.Application
LOCAL loReminder AS Object
loOutlook = CREATEOBJECT('Outlook.Application')
loReminder = loOutlook.Reminders
? “We have “ + TRANSFORM(loReminder.Count) + “ Reminders setup”
? “These are: “
FOR EACH loItem IN loReminder
? “Caption: “ + loItem.Caption + ;
“ DateTime: “ + TRANSFORM(loItem.OriginalReminderDate)
ENDFOR
At this point you can see what the reminders are; you also have the following properties and methods available to you.
NextReminderDate (Property)
IsVisible (Property)
Snooze (Method)
Dismiss (Method)
Snooze takes a variant type parameter which is in minutes; the default is 5 minutes.
Both Snooze and Dismiss will give you an OLE Error if the reminder is not visible, so you would have to check with code like this:
FOR EACH loItem IN loReminder
IF loItem.IsVisible
loItem.Snooze(10) && For 10 Mins
&& Or you could dismiss the Item
&& loItem.Dismiss()
ENDIF
ENDFOR
So far all the Automation commands we have used none of these will cause the Outlook Security dialog to be displayed.
So going back to working with the Inbox, we know how to obtain the number of Messages that are in the Inbox, but what if we wanted to find out how many Read and Unread messages were contained within this folder, the code to do this is as follows:
LOCAL loOutlook AS Outlook.Application
LOCAL loNameSpace AS Outlook.NameSpace
LOCAL loInBox AS Object
LOCAL loMess AS Outlook.MailItem
LOCAL lnUnRead AS Integer
#DEFINE olFolderInBox 6
loOutlook = CREATEOBJECT('Outlook.Application')
loNameSpace = loOutlook.GetNamespace("MAPI")
loInBox = loNameSpace.GetDefaultFolder(olFolderInBox)
lnUnRead = 0
*-- At this point we have the InBox Object.
*-- Let's display how many messages we have in the Inbox.
? "We have " + TRANSFORM(loInBox.Items.Count) + " Messages"
FOR EACH loMess IN loInBox.Items
IF loMess.UnRead
lnUnRead = lnUnRead + 1
? "UnRead Subject: " + loMess.Subject
ELSE
? "Read Message Subject: " + loMess.Subject
ENDIF
ENDFOR
? "Total of UnRead Messages " + TRANSFORM(lnUnRead)
This code also prints the Subject of the Message; also note at this point we still have not seen the Security dialog.
Let’s have a look at some of the methods available to us when we have the Inbox Folder object.
loInBox à Object holding the Inbox Folder.
à Items à This contains all the Mail items within the Inbox Folder.
So using the Items Collection we have the following methods available to us:
|
Method |
Description |
|
GetFirst |
This Returns the First Message in the Folder:
loMess = loInBox.Items.GetFirst |
|
GetLast |
This Returns the Last Message in the Folder:
loMess = loInBox.Items.GetLast,
This would return the newest message in the inbox. |
|
GetNext/ GetPrevious |
These are self explanatory; these allow you to get each message individually working through the inbox. |
|
Find |
This allows you to search the Inbox using a Filter to find the Message you want. The only issue with this is that you cannot locate for a message that contains a string, you need to specify the correct criteria, so to search for a Subject in a message:
loMess = loInBox.Items.Find(‘[Subject]=”Signup Details”’)
You need to check the loMess to see if it actually contains an object, otherwise it will be NULL, if you need to search again, then use the FindNext Method to obtain the next message meeting this criteria. |
So let’s have a go with the above methods:
LOCAL loOutlook AS Outlook.Application
LOCAL loNameSpace AS Outlook.NameSpace
LOCAL loInBox AS Object
LOCAL loMess AS Outlook.MailItem
#DEFINE olFolderInBox 6
loOutlook = CREATEOBJECT('Outlook.Application')
loNameSpace = loOutlook.GetNamespace("MAPI")
loInBox = loNameSpace.GetDefaultFolder(olFolderInBox)
loMess = loInBox.Items.GetFirst()
*-- At this point loMess contains the Object of the First Message in the Inbox.
? loMess.Subject
*-- Let’s move to the next Item
loMess = loInBox.Items.GetNext()
? loMess.Subject
*-- Let’s move to the last message
loMess = loInBox.Items.GetLast()
? loMess.Subject
*-- Let’s see if we can find a message
loMess = loInBox.Items.Find(‘[Subject]=”Signup Details”’)
IF !ISNULL(loMess)
? loMess.Subject
ELSE
? “Cannot find message with Subject ‘Signup Details’”
ENDIF
The MailItem Object is the actual Mail Item, accessing some of the Properties will cause the Security Dialog to be displayed, this grid shows all Methods, Events, Properties. We will be investigating these properties in further detail over the stages of this article.
|
AlternateRecipientAllowed |
Delete |
Move |
Reply |
|
AttachmentAdd |
DeleteAfterSubmit |
NoAging |
ReplyAll |
|
AttachmentRead |
Display |
Open |
ReplyAll |
|
AutoForwarded |
DownloadState |
OriginatorDelivertReport
Requested |
ReplyRecipientNames (*) |
|
AutoResolvedWinner (V11) |
EnableSharedAttachments (H V11) |
OutlookInternalVersion |
ReplyRecipients |
|
BCC (*) |
EntryID |
OutlookVersion |
Save |
|
BeforeAttachmentSave |
ExpiryTime |
Permission (V11) |
SaveAs |
|
BeforeCheckNames |
FlagDueBy |
PermissionService (V11) |
Saved |
|
BeforeDelete |
FlagIcon (V11) |
PrintOut |
SaveSentMessageFolder |
|
BillingInformation |
FlagRequest |
PropertyChange |
Send |
|
Body (*) |
FlagStatus |
Read |
Send |
|
BodyFormat |
FormDescription |
ReadReceiptRequested |
SenderEmailAddress (V11 *) |
|
Categories |
Forward |
ReceivedByEntryID |
SenderEmailType (V11) |
|
CC (*) |
Forward |
RecivedByName (*) |
SenderName (*) |
|
ClearConversationIndex |
HasCoverSheet (H V11) |
ReceivedOnBehalfOf
EntryID |
Sensitivity |
|
Close |
HTMLBody (*) |
ReceivedOnBehalfOfName (*) |
SentOn |
|
Close |
Importance |
ReceivedTime |
SentOnBehalfOfName (*) |
|
Companies |
InternetCodepage |
RecipientReassignment
Prohibited |
Size |
|
Conflicts (V11) |
IsConflict |
Recipients (*) |
ShowCategoriesDialog |
|
ConversationIndex |
IsIPFax (H V11) |
ReminderOverrideDefault |
Subject |
|
ConversationTopic |
ItemProperties |
ReminderPlaySound |
Submitted |
|
Copy |
LastModificationTime |
ReminderSet |
To (*) |
|
CreationTime |
Links |
ReminderSoundFile |
UnRead |
|
CustomAction |
MarkForDownload |
ReminderTime |
VotingOptions |
|
CustomPropertyChange |
MessageClass |
RemoteStatus |
VotingResponse |
|
DeferredDeliveryTime |
Mileage |
Reply |
Write |
Key to Above Table: