Mark Williams
Mark Williams

Reputation: 334

Intercept a mouse click on a hyperlink in a MS Word document in an OLE container?

I am working with Word in an OLE Container in a Delphi form, although the IDE is largely irrelevant to the question.

Up until now I have been unable to find any way of solving the above question and I have created and maintained my own word processor within my app solely because I have been unable to find a way to intercept hyperlink clicks in Word. Embedded MS Word would be the ideal solution save that Word does not expose an onHyperlinkClick event.

My embedded word processor needs to take over control of the hyperlink click as it is not navigating to a specific url, but triggers the running of a fairly elaborate function, which I cannot/do not wish to try to handle from within Word.

In the absence of an MS Word event, I would guess that the only way of doing this would be via a windows hook. I am not very familiar with hooks and would appreciate any feedback as to whether this is the best way to approach the problem. For it to work I would need the hook to be able to:

  1. Identify the mouse coordinates on screen (that I assume is a given)
  2. Intercept and prevent the mouse click to stop MS Word doing its own thing.

In MS Word I would need an exposed function which translates screen coordinates to the location of objects within the word document. I have looked through the Word VBA reference and couldn't find any immediately obvious function for this, but I have a feeling I have seen reference to such a function previously.

Any feedback as to whether this is a possible and sensible approach would be greatly appreciated.

Upvotes: 0

Views: 175

Answers (1)

Mark Williams
Mark Williams

Reputation: 334

I have found the answer to the above. It is fairly complex to achieve. It involves both mouse and keyboard hooks.

VBA for Word has a RangeFromPoint function which you can query to find out the the range below the mouse cursor. You then query the range object to get its hyperlinks.

You cannot query the range in the hook procedure as it causes a threads conflict. I added a timer with an interval of 1 millisecond which is enabled in the hook procedure. This makes life difficult if you want to handle the link yourself. I decided to query on mousedown rather than mousemove as latter likely to eat a lot of resource. As such you have to disable the mouse down each time and simulate it outside of the hook procedure if you want it to be handled in the normal way.

There are also all other sorts of problems to be dealt with such as other windows with a higher z order which may be lying over your ole container. You need to check if cursor falls within these windows.

You also need to make sure you don't handle the mousedown where a special key (such as shift) is depressed at the time of the mousedown otherwise you will disable selecting.

Handling opening of the link by pressing the enter key is much easier.

It would all be much easier if Word offered a hyperlinkclick event!

Upvotes: 0

Related Questions