Intercepting hyperlinks in an embedded Word document

Posted by Ryan on Stack Overflow See other posts from Stack Overflow or by Ryan
Published on 2009-01-23T17:10:47Z Indexed on 2010/05/20 6:10 UTC
Read the original article Hit count: 335

Filed under:
|
|
|

Hello,

I'm working on an app which uses embedded Word documents. We have a feature which allows them to insert a small clickable image into the doc - when the user clicks on it, we want the app to open another window based on some data specified by the user when the image was added. What the application does now is:

  • When the image is inserted, the app creates a hyperlink for it and the data is used as the link destination
  • The user ctrl+clicks on the image and the Word document the WindowSelectionChange event
  • The app handles the WindowSelectionChange event and goes to open the specified window

This approach worked fine with previous versions, when we had the 11.0 / Word 2003 interop dll's. We upgraded to 12.0/Word 2007 for the upcoming release, and in many cases the event is not firing when I click on the image - sometimes it does, sometimes it doesn't, and I'm descending into the cargo-cult world trying to figure out why - sometimes saving and re-opening the document works, sometimes killing the Word process and starting a new one fixes (or breaks) the feature. My guess is there's something going wrong with the WinSelChg event, but I'm not sure what. The usual process we have for applying the event handler is:

try //remove the old one if any
{
    ((Document)myAXFramerControl.ActiveDocument).Application.WindowSelectionChange -= 
    new ApplicationEvents4_WindowSelectionChangeEventHandler(WSC_eventhandlerfunction);
}
catch{}
((Document)myAXFramerControl.ActiveDocument).Application.WindowSelectionChange += 
    new ApplicationEvents4_WindowSelectionChangeEventHandler(WSC_eventhandlerfunction);

Sometimes one or both of these will throw exceptions - usually a NullReferenceException when removing the handler. Adding the handler sometimes throws the "com object that has been separated from its underlying rcw cannot be used" exception, which I don't understand at all - my impression was that this only occurs when you, say, store a reference to the Word application or document and try to use it later.

As it stands the WSC event handler is frequently never run; while I'm happy to fiddle with the app until it works once I can't really expect the same of the users who have been happily using this feature for a while now. Any ideas?

© Stack Overflow or respective owner

Related posts about msword

Related posts about word2007