Welcome To The Home Of The Visual FoxPro Experts  
home. signup. forum. archives. search. google. articles. downloads. faq. members. weblogs. file info. rss. print.
OUTLOOK AUTOMATION PART 2

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:

ConstantValueConstantValueolFolderDeleted3olFolderContacts10olFolderOutbox4olFolderJournal11olFolderSent5olFolderNotes12olFolderInBox6olFolderTasks13olFolderCalendar9olFolderDrafts16

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:

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:

Colour Description   Event   Property   Property Object   Method   ReadOnly

Any Item that contains ‘V11’ – means this is a new property to Outlook 2003.
Any Item that contains ‘H’ – means the item is hidden
Any Item that contains ‘*’ – means the security dialog will be displayed when accessed.

I think that is enough for now, I will continue with this area in the next part of this article, like I said before the Outlook Object model is not small, so this will take time to complete, but hopefully we will all learn something new about Outlook.

ABOUT THE AUTHOR: SIMON ARNOLD

Simon Arnold Simon has worked with FoxPro for over 16 years. He currently works for a company based in Harrogate, North Yorkshire (UK), which specialises in FoxPro, Web and Unix development.
He has a weblog at http://weblogs.foxite.com/simonarnold.

FEEDBACK


Your Name: 
Your Feedback: 

Spam Protection:
Enter the code shown: