我在撰写电子邮件时对收件人进行排序。
如果我有3个收件人(例如),请运行排序宏,然后从“收件人:”文本框中删除收件人,再次运行宏会导致删除的收件人重新显示。当我在第二次运行时单步执行宏时,我可以看到.CurrentItem.To
和Recipients
对象仍然拥有所有3个收件人。
这是断断续续的。有没有办法在编辑To:文本框时强制Recipients
对象更新?
我在Outlook VBA documentation中找不到任何内容,但是试错法已证明无效。
代码摘录:
Public Sub SortRecipients()
With Application.ActiveInspector
If TypeOf .CurrentItem Is Outlook.MailItem Then
Debug.Print "Before: "
Debug.Print "To: " & .CurrentItem.To
Debug.Print "# of recipients: " & .CurrentItem.Recipients.Count
' Force an update if recipients have changed (DOESN'T HELP)
.CurrentItem.Recipients.ResolveAll
Set myRecipients = .CurrentItem.Recipients
' Create objects for To list
Dim myRecipient As recipient
Dim recipientToList As Object
Set recipientToList = CreateObject("System.Collections.ArrayList")
' Create new lists from To line
For Each myRecipient In myRecipients
recipientToList.Add myRecipient.Name
Next
' Sort the recipient lists
recipientToList.Sort
' Remove all recipients so we can re-add in the correct order
While myRecipients.Count > 0
myRecipients.Remove 1
Wend
' Create new To line
Dim recipientName As Variant
For Each recipientName In recipientToList
myRecipients.Add (recipientName)
Next recipientName
.CurrentItem.Recipients.ResolveAll
End If
End With
End Sub
答案 0 :(得分:1)
您可以(并且应)添加Option Explicit
,Outlook会告诉您myRecipients
未在您的代码开头声明。
我补充说:
Dim myRecipients As Recipients
[编辑] 这还不足以让To
字段刷新。我尝试了几件事,但最终,我添加了.CurrentItem.Save
,而不是尝试.CurrentItem.Recipients.ResolveAll
我认为我在Outlook 2007上以这种方式工作。