I am working on creating a macro in Outlook VBA that aims to copy contacts from my default contact-folder to another contact-folder, conditioned by the values of a user-defined field. When refrencing the value of a built in field, the code works as desired. However, the problem arrises when trying to reference the values of a user-defined field. My code seems to be unable to reach the field.
Here, when referencing to the title of the contact ("title" being a built-in field), VBA finds it immediately and there is no problem.
Sub copyitem() ' Copy and move all contacts from default folder "Contacts" to "Contacts.1.01
Dim olookitem As Object
Dim olookname As NameSpace
Dim olookfldr As folder
Dim destfolder As folder
Dim olookcontactitem As ContactItem
Dim mycopieditem As ContactItem
Set olookname = Application.GetNamespace("MAPI")
Set olookfldr = olookname.GetDefaultFolder(olFolderContacts)
Set destfolder = olookfldr.Folders("Contacts.1.01")
For Each olookitem In olookfldr.items
If olookitem.Class = olContact And olookitem.Title = "Mr." Then
Set olookcontactitem = olookitem
Set mycopieditem = olookitem.Copy
mycopieditem.Move destfolder
End If
Next
End Sub
However, when trying to reference which team in my office the contact belongs to (Column named "Team"), which is defined in a user-defined field, my macro fails to recognize it. Code following below.
Sub copyitem() ' Copy and move all contacts from default folder "Contacts" to "Contacts.1.01
Dim olookitem As Object
Dim olookname As NameSpace
Dim olookfldr As folder
Dim destfolder As folder
Dim olookcontactitem As ContactItem
Dim mycopieditem As ContactItem
Dim Myproperty As Object
Set olookname = Application.GetNamespace("MAPI")
Set olookfldr = olookname.GetDefaultFolder(olFolderContacts)
Set destfolder = olookfldr.Folders("Contacts.1.01")
Set Myproperty = olookfldr.UserDefinedProperties.Find("Team")
Set MyP = olookfldr.UserDefinedProperties
' MsgBox (MyP.item(1).Name)
For Each olookitem In olookfldr.items
If olookitem.Class = olContact And MyP.item(9).Name = "Accounting" Then
Set olookcontactitem = olookitem
Set mycopieditem = olookitem.Copy
mycopieditem.Move destfolder
End If
Next
End Sub
CodePudding user response:
User-defined (and all other) properties can be retrieved using ContactItem.PropertyAccessor.GetProperty
. The method takes the DASL property name as the parameter; to see the properties and their DASL names, take a look at an item with that property set with OutlookSpy (I am its author) - click IMessage button, select the property, see its DASL name.
You can also retrieve a user property using ContactItem.UserProperties.Item("PropertyName")
, but the custom property must be added as a user property, while ContactItem.PropertyAccessor.GetProperty
can retrieve any available property.
CodePudding user response:
You need to get the property value of the user-defined field from the contact item, not folder:
Dim userProps as Outlook.UserProperties
Dim userProp as Outlook.UserProperty
For Each olookitem In olookfldr.items
If olookitem.Class = olContact The
Set olookcontactitem = olookitem
Set userProps = olookcontactitem.UserProperties
Set userProp = userProps.Find("Team")
Dim value as String = userProp.Value
If value = "Accounting" Then
Set mycopieditem = olookitem.Copy
mycopieditem.Move destfolder
End If
End If
Next