Microsoft KB Archive/294360

= OL2002: How to Programmatically Update Company Names =

Article ID: 294360

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Outlook 2002 Standard Edition

-



This article was previously published under Q294360





SUMMARY
This article explains how you can use Outlook Visual Basic for Applications to change the company name for many contacts without having to manually open each contact. This solution is useful if a company changes its name and you have many contacts for that particular company.



MORE INFORMATION
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:

https://partner.microsoft.com/global/30000104

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS

To create the Visual Basic for Applications macro, follow these steps:  On the Tools menu, point to Macro, and then click Macros. In the Macro Name box, type ChangeCompanyName (without spaces), and then click Create. This starts the Visual Basic Editor and automatically creates a subroutine for you.  Type the following lines of code so that the Visual Basic for Applications procedure appears as follows: Sub ChangeCompanyName

Dim objContactsFolder As Outlook.MAPIFolder Dim objContacts As Outlook.Items Dim strOldCo As String Dim strNewCo As String Dim objContact As Object Dim iCount As Integer

' Specify which contact folder to work with Set objContactsFolder = Session.GetDefaultFolder(olFolderContacts) Set objContacts = objContactsFolder.Items

' Prompt for old and new company names strOldCo = InputBox(&quot;Enter the old company name.&quot;) strNewCo = InputBox(&quot;Enter the new company name.&quot;)

iCount = 0

' Process the changes For Each objContact In objContacts If TypeName(objContact) = &quot;ContactItem&quot; Then If objContact.CompanyName = strOldCo Then objContact.CompanyName = strNewCo objContact.Save iCount = iCount + 1 End If     End If   Next MsgBox &quot;Number of contacts updated:&quot; & Str$(iCount)

' Clean up  Set objContact = Nothing Set objContacts = Nothing Set objContactsFolder = Nothing

End Sub  On the File menu, click Close and Return to Microsoft Outlook.

To use the macro, follow these steps:
 * 1) On the Tools menu, point to Macro, and then click Macros.
 * 2) Click the ChangeCompanyName macro, and then click Run.
 * 3) Wait until a window appears telling you how many contacts have been updated.

Following are some additional notes to be aware of:  It may take a while to process the items in the folder. While this happens, the mouse pointer does not change to an hourglass indicating Outlook is running the macro. The Outlook object model does not support changing the mouse pointer in this manner.</li> You can assign the macro to a toolbar button if you use this functionality often.For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

292797 OL2002: How to Assign a Macro to a Toolbar Button

</li>  The code above only works with the default Contacts folder. If you wish to have it work with any contacts folder you have selected, change the following line: Set objContactsFolder = Session.GetDefaultFolder(olFolderContacts) to: Set objContactsFolder = Outlook.ActiveExplorer.CurrentFolder If you are familiar with programming and want to use a specific contacts folder in a different location, please see the following article in the Microsoft Knowledge Base for information on how to accomplish that:

290804 OL2002: Programming Examples for Referencing Items and Folders

</li></ul>

<div class="references_section">