OL2002: How to Programmatically Find a User's Next Available Free Time (294461)



The information in this article applies to:

  • Microsoft Outlook 2002

This article was previously published under Q294461

SUMMARY

This article describes how to use Visual Basic or Visual Basic Scripting Edition (VBScript) and the FreeBusy method in Outlook to determine the next free time for a particular recipient.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: The function in this section accepts a string value that contains the alias of the recipient and returns the date and time of the next available free time period according to the recipient's Free/Busy information.

If the alias cannot be resolved, the function returns the invalid date, 1/1/4501. This date is commonly used by Outlook to represent an empty date field. If you programmatically store this date in an Outlook field, the word "None" appears in the field's control.

If the current time period for the recipient is marked as "free," the function returns the time that marked the beginning of the period, which is most likely in the past. For instance, if the current time is 1:43 PM and the recipient is currently marked as "free," this function returns the current date and a time of 1:30:00 PM.

In this sample code, time periods are measured in 30 minute increments. You can change this by modifying the value that is assigned to the intSlotLength variable.

Variable data types are commented for compatibility with VBScript.
Function GetNextFreeTime(strAlias)     ' As Date
   Dim olApplication   ' As Outlook.Application
   Dim olSession       ' As Outlook.NameSpace
   Dim olRecipient     ' As Outlook.Recipient
   Dim intSlotLength   ' As Integer
   Dim strFreeBusy     ' As String
   Dim intCurrentSlot  ' As Integer
   Dim intFreeSlot     ' As Integer
   Dim dblFreeSlot     ' As Double
   Dim dtmFreeSlot     ' As Date

   'get a reference to the Outlook session
   Set olApplication = CreateObject("Outlook.Application")
   Set olSession = olApplication.Session
    
   'get a reference to the recipient
   Set olRecipient = olSession.CreateRecipient(strAlias)
   olRecipient.Resolve

   'if the alias did not resolve, return an invalid date.
   If Not olRecipient.Resolved Then
      GetNextFreeTime = #1/1/4501#
      Exit Function
   End If

   'get the free busy string
   intSlotLength = 30
   strFreeBusy = olRecipient.FreeBusy(Date, intSlotLength, True)
    
   'calculate the position of *now* in the free busy string
   intCurrentSlot = Int(DateDiff("n", Date, Now) \ intSlotLength) + 1

   'get the position of next *free* in the free busy string
   intFreeSlot = InStr(intCurrentSlot, strFreeBusy, "0")
    
   'get the number of minutes into the day for my free slot
   dblFreeSlot = (intFreeSlot - 1) * intSlotLength
   
   'get an actual date/time
   dtmFreeSlot = DateAdd("n", dblFreeSlot, Date)
   GetNextFreeTime = dtmFreeSlot
End Function
				
To use the function, you need to supply the alias of the recipient. You can also supply a resolvable e-mail address or display name. The following line of sample code shows how to display the next free time period in a message box for the current user (where objOutlookNameSpace is a valid Outlook NameSpace object).
MsgBox GetNextFreeTime(objOutlookNameSpace.CurrentUser.Address)
				

REFERENCES

For additional information about available resources and answersto commonly asked questions about Microsoft Outlook solutions, click the article number below to view the article in the Microsoft Knowledge Base:

287530 OL2002: Questions About Custom Forms and Outlook Solutions


Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbhowto KB294461