RESOLUTION
The collection actually contains the correct number of appointments. Therefore, if you are looping through all of the appointments, do not use the
For I = 1 to Items.Count approach. Instead, use the
For..Each construct to loop through the items.
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 following Automation subroutine illustrates both the problem and the approach to correct the problem. Be sure to set a reference to the Microsoft Outlook 8.0 Object Library before running this code.
Sub NumberOf1999Appts()
Dim oOutApp As Outlook.Application
Dim oNS As Outlook.NameSpace
Dim CalFolder As Outlook.MAPIFolder
Dim CalItems As Outlook.Items
Dim ResItems As Outlook.Items
Dim sFilter As String
Dim iNumRestricted As Integer
Dim itm As Object
Set oOutApp = New Outlook.Application
Set oNS = oOutApp.GetNamespace("MAPI")
' Use the default calendar folder
Set CalFolder = oNS.GetDefaultFolder(olFolderCalendar)
' Get all of the appointments in the folder
Set CalItems = CalFolder.Items
' Sort all of the appointments based on the start time
CalItems.Sort "[Start]"
' Make sure to include all of the recurrences
CalItems.IncludeRecurrences = True
'create the Restrict filter to return all 1999 appointments
sFilter = "[Start] >= '" & Format("1/1/1999 12:00am", _
"ddddd h:nn AMPM") & "'" & " And [End] < '" & _
Format("1/1/2000 12:00am", "ddddd h:nn AMPM") & "'"
' Apply the filter to the collection
Set ResItems = CalItems.Restrict(sFilter)
' This will return 2147843647 if any recurring appointment
' does not have an end date
MsgBox ResItems.Count
iNumRestricted = 0
' Loop through the items in the collection. This will not
' loop infinitely.
For Each itm In ResItems
iNumRestricted = iNumRestricted + 1
Next
' Display the actual number of appointments in 1999.
MsgBox iNumRestricted
Set itm = Nothing
Set ResItems = Nothing
Set CalItems = Nothing
Set CalFolder = Nothing
Set oNS = Nothing
Set oOutApp = Nothing
End Sub
REFERENCES
For more information about creating solutions with Microsoft Outlook 97,
please see the following articles in the Microsoft Knowledge Base:
166368 OL97: How to Get Help Programming with Outlook
170783 OL97: Q&A: Questions About Customizing or Programming Outlook