宏需要遍历所有联系人并检查以下字段中是否存在“选中标记”:MyCategory、CIC、MC、Asia、Comm、COMI、CoU、RSC、SC 和 SRC。如果联系人在特定字段中有复选标记,则应将其添加到与该字段同名的相应组中。
但经过评估,遗憾的是我无法将联系人添加到组中。我得到运行时错误“424”(需要对象)的地方。
Sub AddContactsToGroups2()
Dim olApp As Outlook.Application
Dim olNs As Outlook.NameSpace
Dim olFolder As Outlook.Folder
Dim olContact As Outlook.ContactItem
Dim olGroup As Outlook.DistListItem
Dim strField As Variant
Dim blnChecked As Boolean
Set olApp = Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set olFolder = olNs.GetDefaultFolder(olFolderContacts)
For Each olContact In olFolder.Items
For Each strField In Array("MyCategory", "CIC", "MC", "Asia", "Comm", "COMI", "CoU", "RSC", "SC", "SRC")
blnChecked = False
If Not olContact.UserProperties(strField) Is Nothing Then
If olContact.UserProperties(strField).Value = True Then
blnChecked = True
Exit For
End If
End If
Next strField
If blnChecked Then
'Set olGroup = olNs.CreateItem(olDistributionListItem)
Set olGroup = olApp.CreateItem(olDistributionListItem)
olGroup.DLName = strField
olGroup.AddMembers (olContact) ' here I could not add contact to the group, error "424" (Object required).
olGroup.Save
End If
Next olContact
Set olGroup = Nothing
Set olContact = Nothing
Set olFolder = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub
要添加单个联系人,请使用 AddMember 方法(而不是 AddMembers):
olGroup.AddMember olContact
DistListItem.AddMember 方法接受要添加到列表中的收件人对象。您可以使用
CreateRecipient
类的 Namespace
方法创建一个 Recipient
实例。收件人的名称可以是表示收件人的显示名称、别名或完整 SMTP 电子邮件地址的字符串。例如:
Sub AddNewMember()
'Adds a member to a new distribution list
Dim objItem As Outlook.DistListItem
Dim objMail As Outlook.MailItem
Dim objRcpnt As Outlook.Recipient
Set objMail = Application.CreateItem(olMailItem)
Set objItem = Application.CreateItem(olDistributionListItem)
'Create recipient for distlist
Set objRcpnt = Application.Session.CreateRecipient("Eugene Astafiev") // or email address
objRcpnt.Resolve
objItem.AddMember objRcpnt
'Add note to list and display
objItem.DLName = "Northwest Sales Manager"
objItem.Body = "Regional Sales Manager - NorthWest"
objItem.Save
objItem.Display
End Sub