Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/172796

From BetaArchive Wiki

Article ID: 172796

Article Last Modified on 2/12/2007



APPLIES TO

  • Microsoft Outlook 97 Standard Edition



This article was previously published under Q172796

SUMMARY

This article provides an overview of possible solutions for sequentially numbering items in an Microsoft Outlook folder 97. Outlook does not provide a direct way of automatically incrementing a field value.

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 the Microsoft Consult Line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

Many database-oriented programs, such as Microsoft Access 97, provide a way to create uniquely numbered items (or records). In database terminology, this is typically referred to as a primary key.

There are several situations where you might want Outlook to sequentially number items. A form that a helpdesk uses to track task requests is one such example. Each task in a folder would have a unique task number for tracking purposes.

Outlook does not provide a way of manually indexing fields in folders like most database management systems (DBMS). Outlook stores items in MAPI folders, and because these folders are designed to be flexible in terms of what can be placed in them, they are "less structured" than a typical DBMS. The folders are "self-indexing" based on various criteria, such as how often a folder is accessed, and therefore do not have a primary key that you can manually set.

Implementation Strategies

In all of the following scenarios, you have to create VBScript code with event procedures for the form, typically Item_Open. VBScript code in the Item_Open event keeps track of the auto-incrementing number, or ID, for the items. You then store the number assigned to the last form item in some designated location. Choosing the storage location is the main difference between the following strategies.

Examples below auto-increment the ID number in the Mileage field, since the Mileage field is available in all types of Outlook forms. In general, the code placed in the Item_Open event needs to determine if the form is a new form, or whether the form is an existing form. If it is a new form, VBScript code needs to retrieve, increment, and store the ID in the Mileage field. Then the code needs to update the number wherever the "last used" ID is stored so that it is ready for the next new form. If the form is an existing form, the VBScript code typically does nothing related to the ID number.

Increment Number and Re-publishing the Form

This approach uses a published form to keep track of the ID number. Because the Item_Open event executes when a new item is created or when an existing item is opened, the VBScript code has to test to see whether the index needs to be incremented.

The following is example code to provide a starting point for implementing this type of solution. Since VBScript cannot use Outlook constants, the constants are explicitly defined at the beginning of the code.

To implement this solution follow these steps:

  1. Open a new contact form and enter the following code in the VBScript editor. Then publish the form to the Contact folder, being sure to use the name in the third line of VBScript below (myFormName = "FormName").

        olFolderRegistry = 3
        olFolderContacts = 10
        myFormName = "FormName"
    
        Sub Item_Open()
        ' Set the Outlook NameSpace
        Set olns = Application.GetNameSpace("MAPI")
        ' Set the folder to the Contacts folder
        Set myFolder = olns.GetDefaultFolder(olFolderContacts)
        ' #1/1/4501# is Outlook's internal representation of an
        ' empty data value.
        If Item.LastModificationTime = #1/1/4501# Then
           ' Item is a brand new item
           MsgBox "New Item"
           If Item.Mileage = "" Then
              ' We're creating the first item in the folder
              Item.Mileage = 1
           Else
              ' Increment the index in the Mileage field
              Item.Mileage = CStr(CInt(Item.Mileage) + 1)
           End If
           ' The following 3 lines re-publish the form to
           ' the Contacts folder with the new index number
           ' in the Mileage field.
           Set myForm = Item.FormDescription
           myForm.Name = myFormName
           myForm.PublishForm olFolderRegistry, myFolder
        Else
           ' Item is an existing item
           MsgBox "Existing Item, doing nothing"
        End If
        End Sub
                            
  2. Using your right mouse button, click the Contact folder and from the context-sensitive menu, click Properties.
  3. Set the "When posting to this folder, use" option to the name of published form so that the form is the default form for the folder.
  4. Set the view to one of Outlook's "table" views and then using the Field Chooser, drag the Mileage field to the column heading so you can easily see the values in that field.

Use Microsoft Access 97 to Generate the Unique ID

You can use this method as part of a much larger data-synchronizing solution between Outlook and Microsoft Access. For example, you store a master copy of contact data in Microsoft Access. In this case, you match the Mileage field to a primary key field in Microsoft Access.

A custom Outlook contact form can have an Item_Open event in VBScript that checks the Mileage field and then uses Data Access Objects (DAO) to retrieve the information for the contact. If the Mileage field is empty, this means the user created a new contact item and DAO code is used to create a new contact record in the Microsoft Access database. This automatically generates a unique ID for the record, and code can retrieve and store this ID in the Outlook Mileage field.

Use an Item in a Folder

Instead of using a published form, an Outlook item can store the last-used ID number. If you place the item in the same folder as the other numbered items, one issue to consider is that someone might accidentally delete the item storing the last-used number. To safeguard this, you can store the item in a different folder.

Text File

Store the last-used number in a separate text file on the hard drive using the FileSystemObject that is available with VBScript version 2.0.

REFERENCES

For more information about creating solutions with Microsoft Outlook 97, please see the following articles in the Microsoft Knowledge Base:

166368 OL97: How to Get Help Programming with Outlook

170783 OL97: Q&A: Questions about Customizing or Programming Outlook



Additional query words: OutSol OutSol97 count

Keywords: kbhowto kbprogramming KB172796