OL2000: How to Programmatically Update Company Names

ID: Q239711


The information in this article applies to:
  • Microsoft Outlook 2000


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 Solution Provider or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Solution Providers, please see the following page on the World Wide Web:

http://www.microsoft.com/mcsp/
For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp

To create the Visual Basic for Applications macro, follow these steps:
  1. On the Tools menu, point to Macro, and then click Macros.


  2. 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.


  3. Type the following lines of code so that the Visual Basic for Applications procedure appears as follows:


  4. 
    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("Enter the old company name.")
       strNewCo = InputBox("Enter the new company name.")
    
       iCount = 0
    
       ' Process the changes
       For Each objContact In objContacts
          If TypeName(objContact) = "ContactItem" Then
             If objContact.CompanyName = strOldCo Then
                objContact.CompanyName = strNewCo
                objContact.Save
                iCount = iCount + 1
             End If
          End If
       Next
       
       MsgBox "Number of contacts updated:" & Str$(iCount)
    
       ' Clean up
       Set objContact = Nothing
       Set objContacts = Nothing
       Set objContactsFolder = Nothing
    
    End Sub 
  5. 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.


  • You can assign the macro to a toolbar button if you use this functionality often. To do this, click View, point to Toolbars, and then click Customize. Change the Categories to Macros, and then drag the macro onto the toolbar. For more information about how to customize toolbars, please see online Help.


  • 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:
    Q208520 OL2000: Programming Examples for Referencing Items and Folders


REFERENCES

For additional information about available resources and answers to commonly asked questions about Microsoft Outlook 2000 solutions, please see the following article in the Microsoft Knowledge Base:

Q146636 OL2000: Questions About Custom Forms and Outlook Solutions

Additional query words: OutSol OutSol2000 vbscript

Keywords : OffVBS
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: December 29, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.