Developing Smart Tags with .NET 2.0

Question1:
I tried to implement the Visual C#-Tutorial descriped in the Smart Tag SDK Documentation shipped with Microsoft Office 2003 Smart Tag SDK. First I implemented it in Visual Studio 2005. Then I got an error in the Smart Tag Explorer in the Item Details under Observations:
"Error loading the requested managed object: Die Version 2.0 ist nicht kompatibel.". This means sth. like Version 2.0 is not compatible. I think this is because of the .NET Framework 2.0.

Any Ideas, what I have to do to run it with .NET 2.0, or is with Version 2.0 meant sth. other
_____________________

Question2:
So I tried it again in Visual Studio 2003 and got no more errors. But the recognizer and the action still have exclamation marks and the Smart Tag still doesn't work. In the Item Details also the Name, the Description, the ProgID and the Smart Tag count are missing.

I implemented all Interface-methods, did the registry entries, used the batch files to grant full trust. So what is the problem, any Ideas
__________________________________

System:
WinXP Prof. SP2 (german)
Office 2003 SP2 (german)
Visual Studio 2003 (7.1.3088 german)
.NET Framework 1.1 (1.1.4322 SP1)
Visual Studio 2005 (8.0.50727.42 (RTM.050727-4200)
.NET Framework 2.0 (2.0.50727)
Microsoft Office 2003 Smart Tag SDK
__________________________________

The Code:

using System;
using Microsoft.Office.Interop.SmartTag;
using System.Windows.Forms;

namespace SimpleCSharpST
{
    public class CSharpSTRecognizer : ISmartTagRecognizer, ISmartTagRecognizer2
    {
        int numTerms;
        string[] terms = new string[20];          

        #region ISmartTagRecognizer Members

        public string ProgId
        {
            get
            {
                // Create a unique identifier for this recognizer
                // that corresponds to the ProgID of this dll
                return "CSharpSimpleSmartTag.STRecognizer";
            }
        }

        public void Recognize(string Text, IF_TYPE DataType, int LocaleID, ISmartTagRecognizerSite RecognizerSite)
        {
            // Not using this because using Recognize2 method instead.
        }

        public int SmartTagCount
        {
            get
            {
                // Specify the number of smart tag types this recognizer supports.
                // 1 in this case.
                return 1;
            }
        }

        public string get_Desc(int LocaleID)
        {
            // Create a longer description of this recognizer.
            return "CSharp Recognizer recognizes color string in documents";
        }

        public string get_Name(int LocaleID)
        {
            // Add a short phrase that describes this recognizer that will be
            // shown in the Tools>Autocorrect Option>Smart Tags
            // dialog box in an Office smart tag supporting application.
            return "CSharp Color Recognizer";
        }

        public string get_SmartTagDownloadURL(int SmartTagID)
        {
            // For this particular smart tag type, there is no Web site
            // to support a smart tag download URL, so return nothing.
            return "";
        }

        public string get_SmartTagName(int SmartTagID)
        {
            // Provide the name of the smart tag type supported.
            // SmartTag names are always in the format of
            // namespaceURI#tagname.
            if (SmartTagID == 1)
            {
                return "schemas-contoso-com/contoso#color";
            }
            return "";
        }

        #endregion

        #region ISmartTagRecognizer2 Members

        public void DisplayPropertyPage(int SmartTagID, int LocaleID)
        {
        }

        public void Recognize2(string Text, IF_TYPE DataType, int LocaleID, ISmartTagRecognizerSite2 RecognizerSite2, string ApplicationName, ISmartTagTokenList TokenList)
        {
            int i;
            ISmartTagProperties propbag;
            ISmartTagToken STToken;

            //The following is for the case if IndexOf search is needed
            int Index;
            int termlen;

            try
            {
                int nToken;
                for (i = 1; i <= numTerms; i++)
                {
                    if (TokenList != null)
                    {
                        for (nToken = 1; nToken <= TokenList.Count; nToken++)
                        {
                            STToken = TokenList.get_Item(nToken);
                            if (!(STToken == null))

                                //Perform a case-insensitive search through the passed-in string
                                //for each of the terms supplied
                            {
                                if ((STToken.Text).ToLower() == (termsIdea).ToLower())
                                    //Ask the reconizer site for a property
                                {
                                    propbag = RecognizerSite2.GetNewPropertyBag();
                                    //Commit the smart tag to the application
                                    RecognizerSite2.CommitSmartTag2("schemas-contoso-com/contoso#color", STToken.Start, STToken.Length, propbag);
                                }
                            }
                            STToken = null;
                        }
                    }
                }

                //Trap for the case where TokenList Is Nothing
                //Just in case something goes wrong, for example no token breaker is installed
                //and therefore using TokenList won't work
                //or token list is empty for some unforeseen reasons
                //use IndexOf to do the searches
                if (TokenList == null)
                {
                    Text = Text.ToLower();
                    for (i = 1; i <= numTerms; i++)
                    {
                        // Adding 1 because String.IndexOf method is 0-based
                        Index = Text.IndexOf(termsIdea) + 1;
                        termlen = (termsIdea).Length;
                        while (Index > 0)
                            // Ask the recognizer site for a property bag
                        {
                            propbag = RecognizerSite2.GetNewPropertyBag();
                            // Commit the smart tag to the application
                            RecognizerSite2.CommitSmartTag("schemas-contoso-com/contoso#color", Index, termlen, propbag);
                            // Look for another smart tag in the string
                            Index = Text.IndexOf(termsIdea, Index + termlen);
                        }
                    }

                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }

            finally
            {
                // Release the TokenList object and garbage collect at the end
                // To ensure an application doesn't stay in memory
                System.Runtime.InteropServices.Marshal.ReleaseComObject(TokenList);
                GC.Collect();
            }
            return;

        }

        public void SmartTagInitialize(string ApplicationName)
        {
            // Declare these terms in all lowercase so that
            // case-insensitive searches can be performed.
            try
            {
                terms[1] = "purple";
                terms[2] = "blue";
                terms[3] = "green";
                terms[4] = "red";
                terms[5] = "black";
                termsDevil = "white";
                terms[7] = "pink";
                termsMusic = "yellow";
                terms[9] = "gold";
                terms[10] = "brown";
                terms[11] = "maroon";
                numTerms = 11;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public bool get_PropertyPage(int SmartTagID, int LocaleID)
        {
            return false;
        }

        #endregion
    }
}

 



using System;
using Microsoft.Office.Interop.SmartTag;
using System.Windows.Forms;

namespace SimpleCSharpST
{
    class CSharpSTAction : ISmartTagAction, ISmartTagAction2
    {
        #region ISmartTagAction Members

        public void InvokeVerb(int VerbID, string ApplicationName, object Target, ISmartTagProperties Properties, string Text, string Xml)
        {
        }

        public string ProgId
        {
            get
            {
                // Create a unique identifier for this action provider
                // that corresponds to the ProgID of this dll.
                return "CSharpSimpleSmartTag.STAction";
            }
        }

        public int SmartTagCount
        {
            get
            {
                // Specify the number of smart tag types this action DLL
                // supports. 1 in this case.
                return 1;
            }
        }

        public string get_Desc(int LocaleID)
        {
            // Create a longer description of this action.
            return "Provides actions for CSharp color tree data";
        }

        public string get_Name(int LocaleID)
        {
            // Add a short phrase that describes this action provider DLL.
            return "CSharp Color Actions";
        }

        public string get_SmartTagCaption(int SmartTagID, int LocaleID)
        {
            // Specify the title caption to be displayed in the smart tag UI.
            return "CSharp Color Names";
        }

        public string get_SmartTagName(int SmartTagID)
        {
            // Provide the name of the smart tag type supported.
            // SmartTag type names are always in the format of
            // namespaceURI#tagname.
            // Note that the SmartTagID = 1 check isn't strictly necessary
            // since only 1 smart tag type is supported and will only get
            // called once.
            if (SmartTagID == 1)
            {
                return "schemas-contoso-com/contoso#color";
            }
            return "";
        }

        public string get_VerbCaptionFromID(int VerbID, string ApplicationName, int LocaleID)
        {
            return "";
        }

        public int get_VerbCount(string SmartTagName)
        {
            // Specify the number of verbs supported for a given smart tag type.
            // 1 in this example.
            if (SmartTagName == "schemas-contoso-com/contoso#color")
            {
                return 1;
            }
            return 0;
        }

        public int get_VerbID(string SmartTagName, int VerbIndex)
        {
            // For this simple action provider, VerbID is returned back
            // to the action client as the unique ID.
            return VerbIndex;
        }

        public string get_VerbNameFromID(int VerbID)
        {
            // This string is the name for the string that is used
            // internally to represent the VerbID.
            // For example, it is used in the object model, like:
            // Selection.SmartTags("schemas-contoso-com
            // /contoso#color")
            // .Action("Word").Execute
            if (VerbID == 1)
            {
                return "viewColorInfo";
            }
            return "";
        }

        #endregion

        #region ISmartTagAction2 Members

        public void InvokeVerb2(int VerbID, string ApplicationName, object Target, ISmartTagProperties Properties, string Text, string Xml, int LocaleID)
        {
            try
            {
                System.Diagnostics.Process runIE = new System.Diagnostics.Process();

                // Don't write process output to the Process instance's
                // StandardOutput member. This disable writing to a destination
                // other than the standard output stream .
                runIE.StartInfo.RedirectStandardOutput = false;
                // Set the Web site to start.
                runIE.StartInfo.FileName = "http://www.contoso.com/";
                // Assign a window style.
                runIE.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                // Use the operating system shell to start the process.
                runIE.StartInfo.UseShellExecute = true;

                runIE.Start();

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());

            }
            finally
            {
                // Release the TokenList object and garbage collect at the end
                // To ensure an application doesn't stay in memory
                System.Runtime.InteropServices.Marshal.ReleaseComObject(Target);
                GC.Collect();
            }
            return;
        }

        public void SmartTagInitialize(string ApplicationName)
        {
            // TODO: Add CSharpSTAction.SmartTagInitialize implementation
        }

        public bool get_IsCaptionDynamic(int VerbID, string ApplicationName, int LocaleID)
        {
            // TODO:  Add CSharpSTAction.get_IsCaptionDynamic implementation
            return false;
        }

        public bool get_ShowSmartTagIndicator(int VerbID, string ApplicationName, int LocaleID)
        {
            return true;
        }

        public string get_VerbCaptionFromID2(int VerbID, string ApplicationName, int LocaleID, ISmartTagProperties Properties, string Text, string Xml, object Target)
        {
            // Again, this If statement is technically unnecessary since the
            // calls should only be with the IDs supplied in the VerbID
            // property. Still, it's better to be robust and prevent unintended
            // behavior by misbehaving action clients.
            if (VerbID == 1)
                // Use "///" to get cascading menu
            {
                return "Color information ///Go to Web site ";
            }
            return "";
        }

        #endregion
    }
}

 



Answer this question

Developing Smart Tags with .NET 2.0

  • xiaomaolover

    Now I found this:

    http://support.microsoft.com/ scid=kb%3Ben-us%3B907417&x=15&y=9

    That update resolves the problem described in question 1. Now the Smart Tag works as well, when compiled with .NET 2.0. But the Smart Tag Explorer still shows the exclamation mark and the same error...

  • waroop

    Ok, forget the Smart Tag SDK when developing Smart Tags with .NET 2.0, try this:
    http://msdn2.microsoft.com/en-us/library/ms178786(en-US,VS.80).aspx

    I'm going to test this tomorrow...

  • Nick77

    OK, I got it, it was the registration. I'm not sure, whether I missunderstood the Tutorial, or it's the formulation which is wrong.

    The Tut says:
    "Step 1. In the Actions folder at

    HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag\Actions

    create a new key: right-click on the Actions key, point to New, and then click Key.

    Step 2. Enter the action handler’s Namespace.ClassName which in this example is SimpleCSharpST.CSharpSTAction as the new key value."

    So I creted a new key, named it SimpleCSharpST and set the value of this key to SimpleCSharpST.CSharpSTAction. But instead of this, you have to name the key and not the value to SimpleCSharpST.CSharpSTAction to make the Smart Tag working properly.

    But there is still the problem with question 1.

  • Developing Smart Tags with .NET 2.0