You are currently offline, waiting for your internet to reconnect

OL2002: Working with Keywords Fields from VBScript

This article was previously published under Q291117
This article has been archived. It is offered "as is" and will no longer be updated.
This article describes how to programmatically access the contents of bothstandard and custom keywords fields in Microsoft Outlook using MicrosoftVisual Basic Scripting Edition (VBScript). Concepts in this article also apply to solutions based in Visual Basic or Visual Basic for Applications.
More information
Outlook items can contain information that is stored in a keywords format.The format is typically used to represent a grouped list of informationsuch as company names and categories. For example, you might want to use akeywords field to store a list of companies that a salesperson isresponsible for.

However, keywords fields are most commonly associated withthe 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 provides three standard keywords fields.
   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 objectmodel, they are treated like a standard text field. For example, if theCategories 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 VBScript Split function 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)				

User-Defined (Custom) Keywords Fields

It is not possible to directly modify the contents of a user-definedkeyword field using VBScript. Outlook uses a different variation of arraydata 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 amessage box, assign the field to an array variable, or perform anystring-related function on it.

For example, if you create a keywords field called MyKeywords, thefollowing two lines of code will both generate a "Type mismatch" errormessage:
MsgBox Item.UserProperties.Find("MyKeywords").ValueItem.UserProperties.Find("MyKeywords").Value = "New Text"				
The simplest way to work around this limitation is by accessing the textvia 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 acontrol. 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 thebeginning 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, point to Forms and then click Design This 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 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, then close the editor:
    Sub Commandbutton1_Click()   Set MyPage = Item.GetInspector.ModifiedFormPages("Message")   Set MyControl = MyPage.Controls("TextBox1")   MyControl.Value = Now() & ", " & MyControl.Value   MsgBox MyControl.ValueEnd Sub					
  10. On the Form menu, click Run This Form.
  11. Click the command button to run the VBScript code. A new date will be added to the keywords field every time the command button is clicked. Note that Outlook will automatically remove the extra comma at the end of the field when the focus is moved off of the field or when the item is saved.

Using a Standard Keywords Field

If you are not using one of the standard keywords fields in a form, such as the Categories field, then 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 = ""				
For additional information about available resources and answersto commonly asked questions about Microsoft Outlook solutions, click the article number below to view the article in the Microsoft Knowledge Base:
287530 OL2002: Questions About Custom Forms and Outlook Solutions
OutSol OutSol2002

Article ID: 291117 - Last Review: 10/25/2013 23:20:00 - Revision: 2.0

Microsoft Outlook 2002 Standard Edition

  • kbnosurvey kbarchive kbhowto KB291117