我正在运行下面的代码并在 Microsoft Exchange 服务器上进行了很多点击。
这通常会导致此特定代码由于某种原因而崩溃。我在运行时收到各种不一致的 VBA 错误,甚至导致 Outlook 完全崩溃。根据我的经验,该.GetDirectReports
方法在频繁调用时似乎不稳定。
我想知道是否可以针对 Outlook 通讯簿的缓存/本地版本运行以下代码。我经常在 Outlook 中看到“更新通讯录”,所以我知道某处有保存的通讯录。
我能否以某种方式与这个保存的通讯簿进行交互,而不是 ping Exchange 服务器?
Public Sub printAllReports()
Dim allReports As Collection
Set allReports = New Collection
Dim curLevelReports As Collection
Set curLevelReports = New Collection
Dim nextLevelReports As Collection
Set nextLevelReports = New Collection
Dim myTopLevelReport As ExchangeUser
Set myTopLevelReport = getExchangeUserFromString("name to resolve")
'add to both the next level of reports as well as all reports
allReports.Add myTopLevelReport
curLevelReports.Add myTopLevelReport
Dim tempAddressEntries As AddressEntries
Dim newExUser As ExchangeUser
Dim i, j As Integer
'flag for when another sublevel is found
Dim keepLooping As Boolean
keepLooping = False
'this is where the fun begins
Do
'get current reports for the current level
For i = curLevelReports.Count To 1 Step -1
'get all the reports for this person
Set tempAddressEntries = curLevelReports.Item(i).GetDirectReports
'add all reports (note .Count returns 0 on an empty collection)
For j = 1 To tempAddressEntries.Count
Set newExUser = tempAddressEntries.Item(j).getExchangeUser
'with no email or title they probably aren't real? this function checks that
If (isExchangeUserActualEmployee(newExUser) = True) Then
allReports.Add newExUser
nextLevelReports.Add newExUser
keepLooping = True
End If
Next j
Set tempAddressEntries = Nothing
Next i
'reset for next iteration
Set curLevelReports = nextLevelReports
Set nextLevelReports = New Collection
'no more levels to keep going
If keepLooping = False Then
Exit Do
End If
'reset flag for next iteration
keepLooping = False
Loop
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
'do stuff with this information (currently just write to new email, could do other cool stuff)
For i = 1 To allReports.Count
oMail.Body = oMail.Body + allReports.Item(i).name + ";" + allReports.Item(i).JobTitle
'Debug.Print getFirstName(allReports.item(i).name) & " " & getLastName(allReports.item(i).name)
'oMail.Body = oMail.Body + allReports.Item(i).FirstName & " " & allReports.Item(i).LastName & ";" & allReports.Item(i).JobTitle & ";" & allReports.Item(i).Alias & vbCrLf
'Debug.Print allReports.Item(i).PrimarySmtpAddress
Next i
oMail.Display
End Sub