Outlook VBA Items.Count wierdness

时间:2015-05-22 13:20:21

标签: vba outlook outlook-vba

我有一个关于VBA Outlook功能的问题我现在有一个代码,下面的代码在两台独立的计算机上运行。

Option Explicit
Public olApp As Outlook.Application
Public olNs As Outlook.NameSpace
Public olFldr As Outlook.MAPIFolder
Public olItms As Outlook.Items
Public myItms As Outlook.Items
Public myDestFolder As Outlook.Folder
Public olMail As Variant
Public i As Long
Public Sender As Outlook.AddressEntry
Public Number As Long
Public environment As String
Public MyObj As Object
Public MySource As Object
Public file As Variant
Public myFile As Variant
Public textline As Variant
Public Text As Variant
Public identifier As Boolean
Public j As Long
Public k As Long
Public counter As Long

Sub MySub()

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = olFldr.Items
Set myDestFolder = olFldr.Folders("Tradebook")
Set myItms = myDestFolder.Items

On Error GoTo ContinueHere2:

counter = olItms.Count

有些奇怪的是,当它在我的计算机上运行时,它会将olItms.Count视为收件箱中的最新电子邮件,即物品20,000是收到的最新电子邮件。当我在同事计算机上设置它时,它会将olItms.Count解释为第一个收到的电子邮件。为什么是这样?我只想知道为了更多地了解它,因为如果不知道何时以及为什么会改变它,那可能是灾难性的。

1 个答案:

答案 0 :(得分:0)

使用Items类的Sort方法按指定的属性对项目集合进行排序。排序仅影响集合中项目的顺序。它不会影响资源管理器视图中项目的顺序。

Sub SortByRecievedDate() 
  Dim myNameSpace As Outlook.NameSpace 
  Dim myFolder As Outlook.Folder 
  Dim myItem As Outlook.TaskItem 
  Dim myItems As Outlook.Items 

  Set myNameSpace = Application.GetNamespace("MAPI") 
  Set myFolder = myNameSpace.GetDefaultFolder(olFolderTasks) 
  Set myItems = myFolder.Items 
  myItems.Sort "[RecievedTime]", False 
  For Each myItem In myItems 
    MsgBox myItem.Subject & "-- " & myItem.DueDate 
  Next myItem 
End Sub