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

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:
Identify the mouse coordinates on screen (that I assume is a given)
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.

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!

Related

Office-JS Word Add-in Document-to-sidebar communication

I'm working on a taskpane add-in for Microsoft Word. So far, I'm able to place comments inside the Docx coming from the sidebar by abusing the OOXML insertion function.
I'm wondering if it's possible to monitor what the end user does with the comment.Is the commented deleted or accepted by the user? It is also significant to monitor the context of the comment - is that changed significantly?
I've considered the following approaches:
Create a ContentControl for the context of the comment to which events can be hooked that check for selection and changes. The downside here is that these ContentControl affect the UI in word and are probably not understood by our end-user. Are there more 'background' ways to do this without affecting the UI too much?
Create an interaction Hyperlink in the comment. I'm not sure if we can 'catch' this as an event or attach a JavaScript hook to this that actually makes it to the add-in?
Poll the document at set intervals to determine if something has happened to the comment or the context of the comment. I'm not sure what the correct hooks for this would be, and how to accurately localize all the changes across the document.
Is there an approach that I missed?
I'm aware that adding these comments is on the roadmap for the office-js, but this has been the case for a couple of years so I'm not sure what the timeframe is for this.
Edit
#Cindy thanks for taking the time to respond! With your help I've been able to get a functioning proof of concept using the hidden Content Control as a prepended OOXML snippet for the comment (So basically approach 1 combined with the OOXML insertion).
I can now capture some of the interaction by adding a binding to the content control and using the EventType.BindingDataChanged, something like this:
Office.context.document.bindings.addFromNamedItemAsync("MyContentControlTest", "text", {}, function (result) {
if (result.status == "succeeded") {
result.value.addHandlerAsync (
Office.EventType.BindingSelectionChanged, function()
{console.log("Don't touch me");}
)
})
})
It still feels a bit circumspect to attach a binding over a ContentControl, but I have not been able to add event listeners directly to the ContentControl. Given a named Content Control (MyContentControlTest) how could I attach an event like the OnDataChanged? And if i read correctly, this last feature is still in Preview, is there a way to find out what the expected release is?

Extracting attachments from mail box using AutoHotKey

I need to extract the attachments(pdf and .xlsx) from mail box using auto hot key.
Can somebody elaborate the steps for achieving this?
Any links could also be of help.
Thanks
#dheerendra
First, figure out how to do it in a stepwise fashion with just the mouse and/or keyboard (look for shortcut keys or accelerator keys or hot keys - those things with underlines under them). Once you can do it manually, secondly, use AutoHotKey (or any other scripting utility) to automate those steps. These links can help.
AutoHotKey can replicate Mouse clicks at locations:
https://www.autohotkey.com/docs/commands/Click.htm
AutoHotKey can replicate sending Keys from the keyboard:
https://www.autohotkey.com/docs/commands/Send.htm
That should do it, now you just put the steps into the script, maybe with some wait times between steps, or maybe with some error checking, or maybe add some flow control logic, or perhaps get and respond to additional user input, or even a GUI.
But if you have any trouble, show us what you tried (just enough of your code to disclose the troublesome part), and tell us what you expected to happen, and then tell us what actually happened.
EDIT: (Examples of the improvements)
E.g., you could use control logic to save in a place and name it depending on the sender or subject, or a category:
https://www.autohotkey.com/docs/commands/IfExpression.htm
Or you could get simple user input to determine where to save it and what to call it:
https://www.autohotkey.com/docs/commands/InputBox.htm
Or even create a full blown application with a GUI and connections to other data, and use that to establish a complete workflow process.
https://www.autohotkey.com/docs/commands/Gui.htm
Also, you didn't say what Mail client you are using, but I should mention that in addition to scripting mouse and keyboard actions, AutoHotKey lets you easily interact with Microsoft Outlook com interface which can easily expose email message objects and their attachments.
https://www.autohotkey.com/docs/commands/ComObjCreate.htm
Good luck, and Hth,

Office JavaScript API: highlighting text in a document

I'm working on a side project using the Microsoft Office JavaScript APIs. I have been relying on the documentation to find my way around, but I've hit a wall trying to find something in the docs (perhaps it isn't there because it doesn't exist).
Recently I attempting to implement some functionality to highlight some text within a Word document. I don't want to modify the document, mind you; in other words I would rather not use something like ContentControl.insertHtml as that would change the actual content. What I want is to make the text temporarily highlighted (e.g., until the user clicks a "Cancel" button), much like what you see when you perform a search with Ctrl+F (and text matching your search is highlighted in yellow).
Is this possible using the Office JavaScript APIs?
Try getting a reference to the Range object and then setting Range.font.highlightcolor. Have a handler for the Cancel button click event that reverses the color change.
Here is a sample application that uses font.highlightcolor from the Office Javascript API. https://github.com/OfficeDev/Word-Add-in-JS-Redact/

Detect hover in Microsoft Word Add-In

I'm trying to develop an Add-In for Microsoft Word. The basic idea being to load some key words into the document and then when the user hovers over one of the key words a box pops up to show more information, like on Wikipedia or Facebook.
Looking over the api reference I see that the popup box isn't possible, and that's okay I could just make it appear in the Add-In's panel.
I read about bindings, but it seems they can't detect a hover or even a click event? I thought about hacking something together with links but I don't think that would be very UI friendly.
The only thing I can think of to make it kind of possible is to have the user manually highlight the key word (sometimes the key word is more than one word), then have them click a "search" button in the Add-In's panel that would read the selected range and compare it against a database of words. It just seems very anti user compared to the original idea.
Does anyone know of a better way to achieve this?

Is it bad practice to handle the showing of the open file dialog, and other dialogs, from within a custom textbox control?

I am making a custom textbox control and am thinking about adding keybindings in the constructor that execute commands to open and save files. I am also thinking about handling the find and replace dialog from within my textbox control.
Is there a reason I shouldn't do this?
--Edit--
I am planning on only using this control in my current application. One of the reasons I am thinking of doing this is to avoid binding to the textbox's Text property, since this binding seems like it would be just as inefficient as updating a string on the textbox's textchanged event.
Well, flexibility comes to mind. Consider the following scenarios, which would be impossible (or at least difficult) in your control:
You want to handle multiple or different methods of opening a file, but it depends on your application.
You want to use your textbox but limit the functionality -- e.g., Find/Replace is not allowed.
You want to change the behavior of any of that in one application but not the other. For example, in app A you want to tack on an extra slash to the end of the text, but in app B you want to add a custom folder name.
In general, I would consider something more generic. Something like a textbox has a specific purpose; enhancing that purpose is fine, but you're going beyond that. You're taking logic that rightly belongs to the app and putting it on a specific control. That limits what you can do with the control across multiple apps.
Of course, if you're writing a control specifically for one and only one app, you don't need to worry about these things. But I would still consider it a bad practice, myself.