OL98: How to Programmatically Update Company Names (241193)



The information in this article applies to:

  • Microsoft Outlook 98

This article was previously published under Q241193

SUMMARY

This article explains how you can use Microsoft Outlook Visual Basic Scripting Edition (VBScript) 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: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: To create a custom Outlook form containing VBScript code to change the contacts, follow these steps:
  1. In your Contacts folder, open a new Contact item.
  2. On the Tools menu, click Forms, and then click Design This Form.
  3. On the Form menu, click Display This Page to hide the default General page.
  4. Click the (P.2) page of the form.
  5. On the Form menu, click Control Toolbox. On the Control Toolbox, click and drag a CommandButton onto the P.2 page of the form.
  6. Right-click the command button, and click Properties. Change the Caption to Change Company Names, and then click OK.
  7. Resize the command button so that the entire caption appears on the form.
  8. On the Form menu, click View Code.
  9. In the Script Editor, type the following VBScript code, and then close the Script Editor:
    Sub CommandButton1_Click()
    
       Dim objNS
       Dim objContactsFolder
       Dim objContacts
       Dim strOldCo
       Dim strNewCo
       Dim objContact
       Dim iCount
    
       ' Specify which contact folder to work with
       Set objNS = Application.GetNamespace("MAPI")
       Set objContactsFolder = Application.ActiveExplorer.CurrentFolder
       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 Left(objContact.MessageClass, 11) = "IPM.Contact" Then
             If objContact.CompanyName = strOldCo Then
                objContact.CompanyName = strNewCo
                objContact.Save
                iCount = iCount + 1
             End If
          End If
       Next
       
       MsgBox "Number of contacts updated: " & CStr(iCount)
    
       ' Clean up
       Set objContact = Nothing
       Set strContacts = Nothing
       Set objContactsFolder = Nothing
       Set objNS = Nothing
    
    End Sub
    					
  10. On the Tools menu, point to Forms, and then click Publish Form. In the Publish Form As window, enter Change Company Name Form as the Display name of the form. Make sure the form is set to be published in your Contacts folder, and then click Publish.
  11. Close and do not save changes to the form.
To use the form, follow these steps:
  1. On the Actions menu, click New Change Company Name Form.
  2. Click the Change Company Names button.
  3. When you are prompted, type the old and new names for the company.
  4. Wait until a window appears telling you how many contacts have been updated.
The 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.
  • The code above works with the current folder you have selected. Therefore, you can publish the form to any contacts folder. Be sure that you don't switch folders after you open the form.

REFERENCES

For additional information about creating solutions with Microsoft Outlook 98, click the article numbers below to view the articles in the Microsoft Knowledge Base:

180826 OL98: Resources for Custom Forms and Programming

182349 OL98: Questions About Custom Forms and Outlook Solutions


Modification Type:MajorLast Reviewed:5/13/2002
Keywords:kbhowto kbProgramming KB241193