OL97: Working with Keywords Fields from VBScript

Last reviewed: March 2, 1998
Article ID: Q174475
The information in this article applies to:
  • Microsoft Outlook 97

SUMMARY

This article describes how to programmatically access the contents of both standard and custom keywords fields in Microsoft Outlook 97 using Microsoft Visual Basic Scripting Edition (VBScript). Concepts in this article may also apply to solutions in which Outlook is automated using Microsoft Visual Basic or Visual Basic for Applications.

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 engineers 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 fee-based consulting 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:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

Outlook items can contain information that is stored in a keywords format. The format is typically used to represent a grouped list of information, such as company names and categories. For example, you might want to use a keywords field to store a list of companies that a salesperson is responsible for. However, keywords fields are most commonly associated with the standard Categories field, which is available on all Outlook forms. This field is used to categorize items even though they are not stored in the same location.

Standard Keywords Fields

Outlook 97 provides three standard keywords fields that can be modified.

   Field name   Forms available in:
   ----------   ------------------------------------------
   Categories   All
   Companies    Contact, Task, Task Request, Journal Entry
   Children     Contact

NOTE: Task items also have a Contacts keywords field, but this field is read-only and therefore cannot be used in solutions.

When accessing these standard keywords fields through the Outlook object model, they are treated like a standard text field. For example, if the Categories selected for a contact are Personal, Phone Calls, and Waiting, the following line of VBScript code will set the MyCategories variable equal to the entire list of categories:

   MyCategories = Item.Categories

You can use the Split function in VBScript version 2.0 to assign individual elements of the field to an array variable. The following sample code takes the three keywords and places them into the first three elements of array MyArray:

   ' Chr(44) is the ANSI value of a comma.
   ' Chr(32) is the ANSI value of a space.
   ' Together, this is the delimiter for a keywords field.
   MyArray = Split(Item.Categories, Chr(44) & Chr(32))
   MsgBox MyArray(0)
   MsgBox MyArray(1)
   MsgBox MyArray(2)

The Split function is not available with VBScript version 1.0, which is the version that is included with all versions of Outlook 97.

For more information about obtaining VBScript version 2.0 and other general information about VBScript, please see the following article in the Microsoft Knowledge Base:

   Article-ID: Q167138
   Title     : OL97: General Information About Using VBScript with Outlook

User-Defined (Custom) Keywords Fields

It is not possible to directly modify the contents of a user-defined keyword field using VBScript. Outlook uses a different variation of array data type than that supported by VBScript, and therefore a "Type mismatch" error message will appear if you try to display the text of the field in a message box (MsgBox), assign the field to an array variable, or perform any string-related function on it.

For example, if you create a keywords field called MyKeywords, the following two lines of code will both generate a "Type mismatch" error message:

   MsgBox Item.UserProperties.Find("MyKeywords").Value
   Item.UserProperties.Find("MyKeywords").Value = "New Text"

You can work around this limitation by accessing the text via a control or by using a standard keywords field.

Accessing the Text Via a Control:

You can work around this limitation by "filtering" the text through a control. For example, you can place a text box (Textbox1) on a form page (P.2) and bind this text box to a user-defined keywords field (MyKeywords). The following steps create sample code that adds the word "New" to the beginning of the MyKeywords field.

NOTE: If you do not want to display the text box used to gain access to the keywords text, you can right-click the text box, click Properties, and clear the Visible property on the Display tab of the Properties window.

  1. Open a new, standard mail message.

  2. On the Tools menu, click Design Outlook Form.

  3. Click the message area of the form to select the message control, then drag the top of the control down to make room for additional controls.

  4. In the Field Chooser, click New.

  5. In the New Field window, type "MyKeywords" (without the quotation marks) in the Name box, click Keywords in the Type list, and then click OK.

  6. Drag the MyKeywords field from the Field Chooser to the blank area above the message control.

  7. From the Toolbox, drag a command button (CommandButton) to the form.

  8. On the Form menu, click View Code.

  9. Type the following code into the Script Editor:

           Sub Commandbutton1_Click()
              Set MyPage = Item.GetInspector.ModifiedFormPages("Message")
              Set MyControl = MyPage.Controls("TextBox1")
              MyControl.Value = "New " & MyControl.Value
              MsgBox MyControl.Value
           End Sub
    
    

  10. On the Tools menu, click Design Outlook Form.

  11. Click the command button to run the VBScript code. The value of the MyKeywords field will change. This can be verified by entering design mode, displaying the All Fields page of the form, and viewing the contents of the field.

Using a Standard Keywords Field:

If one of the standard keywords fields is not being used and it is available based on the type of form you are using (see the Standard Keywords Fields table earlier in this article), you can use that field to gain access to the text in the field. You can temporarily assign the value of your custom keywords field to the standard field, modify the text while it is in the standard field, assign the standard field back to your custom keywords field, and then delete the contents of the standard field you temporarily used. The following sample code provides an overview of this process:

   ' Assign the custom keywords field (MyKeywords) to Categories.
   Item.Categories = Item.UserProperties.Find("MyKeywords")

   ' Modify the field.
   Item.Categories = "New " & Item.Categories

   ' Display the modified field.
   MsgBox Item.Categories

   ' Replace the modified field.
   Item.UserProperties.Find("MyKeywords") = Item.Categories

   ' Reset the Categories field.
   Item.Categories = ""

REFERENCES

For more information about using fields and controls with VBScript, please see the following article in the Microsoft Knowledge Base:

   Article-ID: Q168975
   Title     : How to Use Fields and Controls with VBScript

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

   Article-ID: Q166368
   Title     : OL97: How to Get Help Programming with Outlook

   Article-ID: Q170783
   Title     : OL97: Q&A: Questions about Customizing or
               Programming Outlook


Additional query words: OutSol OutSol97
Keywords : kbcode kbprg
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: March 2, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.