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) |