Change Outlook Contacts “File As” En Masse
Posted by William Diaz on December 6, 2011
In Outlook, there is no native built-in method for changing the “File As” type for all contacts at once. Normally, if you need to do this, you open the contact and change to the File As field for each contact.
Think of someone with several hundred or several thousand contacts who, all of a sudden, realizes that he rather look up his contacts by FirstName instead of LastName (or vice versa). Or take someone who has been filing as one type for a while now and then decided to start filing as a different type later and is then presented with a scenario like that below where sorting by the File As column mixes last and first name alphabetically:
You can work around this with a macro and a little bit of VBA. You may want to enable the Developer Ribbon before hand for Office 2007/2010. Once you have, create a new module or insert the code below into “ThisOutlookSession”. Comment out the strFileAs line that contains the format you are looking to change to and Save. Edit the message box to whatever you want. Note, the larger the number of contacts, the longer this will take. I tested with about 300+ contacts and it takes about 30-45 seconds on a decently juiced workstation. Note, if you run without commenting out the desired format, you simply remove the File As type altogether.
|Public Sub ChangeFileAs()MsgBox “This can take several minutes to complete depending on the number of contacts. Outlook will remain unresponsive until this is completed.”
Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim objContact As Outlook.ContactItem
Dim objItems As Outlook.Items
Dim objContactsFolder As Outlook.MAPIFolder
Dim obj As Object
Dim strFirstName As String
Dim strLastName As String
Dim strFileAs As String
On Error Resume Next
Set objOL = CreateObject(“Outlook.Application”)
Set objNS = objOL.GetNamespace(“MAPI”)
Set objContactsFolder = objNS.GetDefaultFolder(olFolderContacts)
Set objItems = objContactsFolder.Items
For Each obj In objItems
‘Test for contact and not distribution list
If obj.Class = olContact Then
Set objContact = obj
‘ Uncomment the strFileAs line for the desired format
‘Lastname, Firstname (Company) format
‘ strFileAs = .FullNameAndCompany
‘Firstname Lastname format
‘ strFileAs = .FullName
‘Lastname, Firstname format
‘ strFileAs = .LastNameAndFirstName
‘Company name only
‘ strFileAs = .CompanyName
‘Companyname (Lastname, Firstname)
‘ strFileAs = .CompanyAndFullName
.FileAs = strFileAs
Set objOL = Nothing
Set objNS = Nothing
Set obj = Nothing
Set objContact = Nothing
Set objItems = Nothing
Set objContactsFolder = Nothing
You may encounter the following when trying to run the macro: “The macros in this project are disabled. Please refer to the online help…”
To overcome this, you will need to adjust Outlook’s macro security. This can be done from the Developer Ribbon under Macro Security or by going to the File Ribbon > Options > Trust Center > Trust Center Settings > Macro Settings > and changing to Notification for all macros or Enable all macros (not recommended). You will need to exit Outlook for the change to take effect.
Alternatively, if you trust yourself (of course you do), you can self-sign the macro with a personal certificate to avoid this altogether. This comes in handy for macros that you need to run often. To do this,
Go into C:\Program Files (x86)\Microsoft Office\Office14 (32-bit Office).
Run SELFCERT.exe and name your macro certficate.
Attach the certificate to the macro by going into the Visual Basic editor in Outlook.
Select the project where the vba code is.
Go to Tools and select Digital Signature.
Select Choose and select the certificate you just created.
When you run your macro for the first time, you will be prompted by the following message:
Click the Trust all documents from this publisher to avoid this going forward. You can also view all your trusted macros from the Trust Center as well: