index > Visual Studio Tools for Office > Can I get the text Range in smart tag Recognize2 method?

Can I get the text Range in smart tag Recognize2 method?

I am writing a smart tag recognizer for Word 2007. In the Recognize2 method, I want to get the location of the text so I can recognize different tokens in different parts of the document. However, I can't find any way to get a Range object in the Recognize2 method. Is this possible?

For example, I only want to recognize certain tokens when they are in a table. If I could get a Range object for the Text, I could use Range.Information[wdWithInTable] to test if the text is in a table.

Dave Jenkins

The entire Word OM can not and should not be accessed from within the Recognize or Recognize2 methods. This is because those methods are executed on a background thread and hence live in a different COM Apartment than the rest of Word objects (e.g. Application, Document, Range etc).

However, I think there is a technique that could fit your purposes.

The idea is that you can have 2 smart tag types - one is public (e.g. "mycompany#public") and another one is hidden (e.g. "mycompany#private"). Only your public type will have a corresponding SmartTag Action - this will cause the hidden smart tags to be invisible to the end user since Word underlines only smart tags for which there is a corresponding action. You will also need to have a COM AddIn running. The purpose of the COM AddIn is to process Document.SmartTags collection on the main UI thread.

Your Recognize method will only recognize words based on their text. Once it recognizes a sentence it commits the "hidden" smart tag type on int and it also posts a message to your COM AddIn - the message basically tells the COM AddIn that there is a word recognized and additional processing is required. Upon receiving the message COM AddIn can retrieve all the hidden SmartTags using ThisDocument.SmartTags.SmartTagsByType("mycompany#hidden"). Then it can iterate through all these "hidden" smart tags, get the SmartTag.Range object and examine it, then delete the smart tag, and for the ranges that meed your criteria use Range.SmartTags.Add("mycompany#public") API to add the smart tag to the range.

Hope this helps.




Misha (VSTO team is hiring. We would like to hear from you http://tinyurl.com/ZQGW2)
Misha Shneerson - MSFT
Wow, that should work. My only worry is after you delete the private tag, what prevents Office from recognizing it again and going into an infinite loop?
Dave Jenkins

This is a valid concern although I think just replacing the ST type should not cause Recognizer to re-fire.




Misha (VSTO team is hiring. We would like to hear from you http://tinyurl.com/ZQGW2)
Misha Shneerson - MSFT
What about deleting the private smart tag and not replacing it with anything? That's what I would do if the smart tag doesn't match my criteria to create a public smart tag. Would that cause the Recognizer to re-fire and create an infinite loop?
Dave Jenkins
I think anything that does not touch the text should not trigger the recognizer. But I am only superficially familiar with how smart tags are built - meaning I do not work with Office's code base since I am a developer in Visual Studio. The best way to find out is to try.


Misha (VSTO team is hiring. We would like to hear from you http://tinyurl.com/ZQGW2)
Misha Shneerson - MSFT
Thanks for your help and great suggestion for a way around the background thread problem. As you suggest, I'll give it a try and post the results.
Dave Jenkins

Hi Dave

In my (albeit somewhat limited) experience with SmartTags in Word, using the Delete method should work OK as far as going into an infinite loop is concerned. As with spelling, Word should ignore any unchanged text range where the SmartTag has been removed. Only activating "Recheck document" in Tools/autoCorrect Options/SmartTags should bring it back.




-- Cindy Meister (Word MVP)
Cindy Meister
reply 8

You can use google to search for other answers

 

More Articles

• picture in vdx file (xml visio)
• Print an excel file
• Problem using PIA interop Assemblies for Modules and Forms in an ...
• Combo box and Cc field... HELP!!!
• how to add custom images to command bar buttons in C#
• auto_close addin unload
• VSTO Outlook Addin - How to disable registration after build
• Is it possible to host outlook calendar in a winform app?
• Position cursor after inserted nodes
• Draw my own squigglies on Word document...
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Modified helpbar.
• I need the office tools for VS forum.
• TrackRevisions method not available error
• Integrate my aplication
• VSTO
• Smart Tags in Outlook 2003
• how to insert datagridview into a bookmark
• Outlook VSTO Addin not loaded at startup
• C# Word add-in...weird error
• BUG, OutLook 2003 Add-in only installs f
• Outlook Addin Deployment VSTO
• Action Pane
• Shared Add-In
• Unable to access "Page Brak View&qu
• Outlook Add-in Not Loaded :runtime error

Hot Articles

• NewSheet event not always raised
• Excel Macros
• How to Search Outlook Mail Body?
• Bookmarks in VSTO
• Outlook add-in error 80010106
• Add VSTORuntime and PIA to the bootstrap
• Office graphs links
• VSTO and Windows application
• Outlook Add-in configuration
• newbie in smart clients
• programmatically access offline exchange
• Help Required
• VSTO problem resolution techniques
• How to use the Events of Outlook.Applica
• Copying RTF into word and from word to a

Recommend Articles

• outlook plug-in help
• Getting contact information from Outlook
• ThisApplication_Startup not being called
• Replacing text
• Package Wizard etc
• Excel OLE Link Using VSTO
• bind excel sheet with database without u
• How to select a task pane at startup?
• How to generate XSLT file from WordProce
• Document-level VSTO in Access 2007
• Launching Form in Outlook Add-in
• Definig custom help for user defined fun
• Outlook, contact form
• Issue with launching Word 2003 XML (Word
• Global Address list - microsoft outlook