Copy field value in Contacts

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to copy the value of the Nickname field to the Pager field of each
contact in the Contacts-folder.

IOW: loop through the contacts-collection and copy any value in the Nickname
field to the Pager field of that contact, then empty the Nickname field.

I'm familiar with VBA; just don't know the exact syntax of the Outlook
object model.

Thanks in advance.

Jay
 
See http://www.outlookcode.com/d/code/convertfields.htm for a code sample that should be useful. When in doubt about the exact property names, check the object browser: Press ALt+F11 to open the VBA environment in Outlook, then press F2. Switch from <All Libraries> to Outlook to browse all Outlook objects and their properties, methods, and events. Select any object or member, then press F1 to see its Help topic.
 
Jay, you can see the syntax in the Object Browser (F2). Interesting for
you: ContactItem, Nickname, PagerNumber, Save.
 
Thank you Michael and Sue,

with some c & p and F1 + F2, I managed to make it work.

Just in case anyone else needs it:

================================================

Sub ConvertFields()
Dim objApp As Application
Dim objNS As NameSpace
Dim objFolder As MAPIFolder
Dim objItems As Items
Dim objItem As Object
Dim lngcounter As Long

Set objApp = CreateObject("Outlook.Application")
Set objNS = objApp.GetNamespace("MAPI")
Set objFolder = objNS.GetDefaultFolder(10)

lngcounter = 0

If Not objFolder Is Nothing Then
Set objItems = objFolder.Items
For Each objItem In objItems
' make sure you have a Contact item
If objItem.Class = olContact Then
If IsNull(objItem.NickName) = True Or objItem.NickName = "" Then

Else
objItem.PagerNumber = objItem.NickName
objItem.NickName = ""
objItem.Save
lngcounter = lngcounter + 1
End If
End If
Next
End If

MsgBox "'" & lngcounter & "' nicknames copied to pager-field."

Set objItems = Nothing
Set objItem = Nothing
Set objFolder = Nothing
Set objNS = Nothing
Set objApp = Nothing
End Sub

================================================
 
Thanks for the sample, Jay.

Set objApp = CreateObject("Outlook.Application")

From within OL you shouldn´t call this. In OL 2003 you could run into
security prompts and it seems that this line could cause that VBA will
be disabled.
If IsNull(objItem.NickName) = True Or objItem.NickName = ""

Because NickName is a String type it will never be Null. So it´s enough
to test for NickName="" only. In your case you could write this (the
Else case isn´t necessary):

If objItem.NickName <> "" Then
....
Endif

Or this (faster):

If Len(objItem.NickName)=0 then
....
Endif
 
Back
Top