This article was previously published under Q269172
On This Page
SUMMARY
This article describes how to use a custom Outlook form and Visual Basic Scripting Edition (VBScript) to programmatically change the File As field for a large number of existing contacts.
IMPORTANT: If you change the format of the File As field by using the standard Outlook contact form, Outlook ensures that the format of this field does not affect other areas where this type of information is displayed (such as at the top of a contact when you view it in the Address Cards view). However, this example simply changes the File As field itself and therefore does not perform the same function as the Outlook contact form. Microsoft recommends that you make a copy of your contacts folder and then use the sample code below on the folder that you copied to ensure that the benefits of using this sample outweigh any potential shortcomings of this approach.
NOTE: When you change the File As field, the order in which the contacts appear in the Outlook Address Book is not affected.
Microsoft provides programming examples for illustration only, without warranty either
expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes
that you are familiar with the programming language being demonstrated and the
tools used to create and debug procedures. Microsoft support professionals can
help explain the functionality of a particular procedure, but they will not
modify these examples to provide added functionality or construct procedures to
meet your specific needs. If you have limited programming experience, you may
want to contact a Microsoft Certified Partner or the Microsoft fee-based
consulting line at (800) 936-5200. For more information about Microsoft Certified
Partners, please visit the following Microsoft Web site:
On the File menu, point to New, and then click Mail Message to open a new e-mail message.
2.
On the Tools menu of the new e-mail message, point to Forms, and then click Design This Form.
3.
Insert five Command buttons on the new form. To do this:
a.
Click the (P.2) tab to go to a blank page on the form.
b.
On the Form menu, click Control Toolbox, click CommandButton, and then drag the button to the blank form page.
c.
Right-click the new button, click Properties, and then type cmdLastFirst in the Name box.
d.
In the Caption box, type Last, First, and then click OK.
e.
In the Toolbox dialog box, click CommandButton, and then drag a second button to the blank form page.
f.
Right-click the new button, click Properties, and then type cmdFirstLast in the Name box.
g.
In the Caption box, type First Last, and then click OK.
h.
In the Toolbox dialog box, click CommandButton, and then drag a third button to the blank form page.
i.
Right-click the new button, click Properties, and then type cmdCompany in the Name box.
j.
In the Caption box, type Company, and then click OK.
k.
In the Toolbox dialog box, click CommandButton, and then drag a fourth button to the blank form page.
l.
Right-click the new button, click Properties, and then type cmdLastFirstCompany in the Name box.
m.
In the Caption box, type Last, First (Company), and then click OK.
n.
In the Toolbox dialog box, click CommandButton, and then drag a fifth button to the blank form page.
o.
Right-click the new button, click Properties, and then type cmdCompanyLastFirst in the Name box.
p.
In the Caption box, type Company (Last, First), and then click OK.
4.
Type the following Visual Basic Scripting Edition (VBScript) code. To do this:
a.
On the Form menu, click View Code to open the Script Editor.
b.
In the Script Editor, type or copy the following code:
Option Explicit
Dim strSortBy
Sub cmdLastFirst_Click()
strSortBy = "LastFirst"
UpdateContacts
End Sub
Sub cmdFirstLast_Click()
strSortBy = "FirstLast"
UpdateContacts
End Sub
Sub cmdCompany_Click()
strSortBy = "Company"
UpdateContacts
End Sub
Sub cmdLastFirstCompany_Click()
strSortBy = "Last, First (Company)"
UpdateContacts
End Sub
Sub cmdCompanyLastFirst_Click()
strSortBy = "Company (Last, First)"
UpdateContacts
End Sub
Sub UpdateContacts()
Dim CurFolder
Dim MyItems
Dim MyItem
Dim NumItems, i
' Use whichever folder is currently selected
Set CurFolder = Application.ActiveExplorer.CurrentFolder
' Make sure it's a contact folder
If CurFolder.DefaultItemType = 2 Then
MsgBox "This process may take some time. You will be " & _
"notified when complete.", , "Contact Tools Message"
Set MyItems = CurFolder.Items
NumItems = MyItems.Count
For i = 1 to NumItems
Set MyItem = MyItems.Item(i)
' Make sure it's not a distribution list in the folder
' (really only applies to OL98 and OL2000)
If TypeName(MyItem) = "ContactItem" Then
Select Case strSortBy
Case "LastFirst"
If MyItem.LastNameandFirstName <> "" Then
MyItem.FileAs = MyItem.LastNameandFirstName
Else
MyItem.FileAs = MyItem.CompanyName
End IF
Case "FirstLast"
If MyItem.Subject <> "" Then
MyItem.FileAs = MyItem.Subject
Else
MyItem.FileAs = MyItem.CompanyName
End IF
Case "Company"
If MyItem.CompanyName <> "" Then
MyItem.FileAs = MyItem.CompanyName
Else
MyItem.FileAs = MyItem.LastNameandFirstName
End IF
Case "Last, First (Company)"
MyItem.FileAs = MyItem.LastNameAndFirstName
If MyItem.CompanyName <> "" Then
If MyItem.FileAs <> "" Then
MyItem.FileAs = MyItem.FileAs & " (" & _
MyItem.CompanyName & ")"
Else
MyItem.FileAs = MyItem.FileAs & _
MyItem.CompanyName
End If
End If
Case "Company (Last, First)"
MyItem.FileAs = MyItem.CompanyName
If MyItem.LastNameandFirstName <> "" Then
If MyItem.FileAs <> "" Then
MyItem.FileAs = MyItem.FileAs & " (" & _
MyItem.LastNameAndFirstName & ")"
Else
MyItem.FileAs = MyItem.FileAs & _
MyItem.LastNameAndFirstName
End If
End If
End Select
MyItem.Save
End If ' check TypeName
Next
MsgBox "Finished updating contacts."
Else
MsgBox "The current folder must be a contacts folder."
End If ' check contacts folder
Set MyItem = Nothing
Set MyItems = Nothing
Set CurFolder = Nothing
End Sub
c.
On the File menu in Script Editor, click Close to return to the form.
d.
Click the Message page of the form.
e.
On the Form menu, click Display This Page. This hides the form page so that it does not appear when the form is used.
5.
Publish the form. To do this:
a.
On the Tools menu, point to Forms, and then click Publish Form As.
b.
Verify that next to the Look in button you see Personal Forms Library.
c.
In the Display name box, type a discriptive name for your new form (such as, Change File As fields), and then click Publish.
d.
When you are prompted to save the form definition with the item, click No.
For additional information about available resources and answersto commonly asked questions about Microsoft Outlook solutions, click the article number below
to view the article in the Microsoft Knowledge Base:
146636 (http://support.microsoft.com/kb/146636/EN-US/) OL2000: Questions About Custom Forms and Outlook Solutions
Need More Help? Contact a Support professional by E-mail, Online or Phone.
Customer Service For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
Newsgroups Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.