SmartTagRecognizeContext.PersistTag Method

Stores information about the smart tag.

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word (in Microsoft.Office.Tools.Word.dll)

void PersistTag(
	int startIndex,
	int length,
	ISmartTagProperties propertyBag
)

Parameters

startIndex
Type: System.Int32
The position in the paragraph where the smart tag begins.
length
Type: System.Int32
The length of the smart tag.
propertyBag
Type: Microsoft.Office.Interop.SmartTag.ISmartTagProperties
A property bag containing key and value pairs for the token. Can be null.

ExceptionCondition
InvalidOperationException

PersistTag was not called from the Recognize method.

Call PersistTag from an implementation of the Recognize method to indicate that the smart tag was found in the text. Use the propertyBag parameter to commit any custom properties for the smart tag. You can use these properties to customize the action taken when an item from the smart tag shortcut menu is selected.

The following code example demonstrates how to call PersistTag from an implementation of the Recognize method. This implementation compares each smart tag term to the contents of the paragraph. For each smart tag term in the paragraph, the code adds a custom smart tag property and then uses the PersistTag method to recognize the smart tag. This example assumes that you have added a reference to Microsoft.Office.Interop.SmartTag from the .NET tab of the Add Reference dialog box. This code example is part of a larger example provided for the ISmartTagExtension interface.


void ISmartTagExtension.Recognize(string text, ISmartTagRecognizerSite site, ISmartTagTokenList tokenList, 
    SmartTagRecognizeContext context)
{

    foreach (string term in smartTagDemo.Terms)
    {
        // Search the text for the current smart tag term.
        int index = text.IndexOf(term, 0);

        while (index >= 0)
        {
            // Create a smart tag token and a property bag for the recognized term.
            ISmartTagProperties propertyBag = site.GetNewPropertyBag();

            // Write a new property value.
            string key = "Key1";
            propertyBag.Write(key, DateTime.Now.ToString());

            // Attach the smart tag to the term in the document
            context.PersistTag(index, term.Length, propertyBag);

            // Increment the index and then find the next instance of the smart tag term.
            index += term.Length;
            index = text.IndexOf(term, index);
        }
    }
}


Community Additions

ADD
Show: