I am currently working on developing an addin for Microsoft Word that can read the entire line of text in a Word document and compare it to a dictionary on each keypress event. The goal is to capture the text input by the user, save it to a variable, and still allow the keypress to function as usual within Word. How can I create a function that triggers every time a key is pressed?
Since this is my first time coding a Word addin using Javascript, there may be something obvious that I have overlooked. Please provide guidance as if I am a beginner in this area.
To start, I used this initial file as a reference: https://learn.microsoft.com/en-us/office/dev/add-ins/quickstarts/word-quickstart?tabs=visualstudio
Although I attempted to incorporate the following code snippets into my project, they did not yield any results:
var doc = Office.context.document;
doc.addHandlerAsync(Office.EventType.DocumentSelectionChanged, function () {
insertEmersonQuoteAtSelection;
console.log('Ran new function.')
});
In addition to the above code, I also included the following lines along with its associated context, but again, no changes were observed:
(function () {
Office.onReady(function () {
// Office is ready.
$(document).ready(function () {
// The document is ready.
// Use this to check whether the API is supported in the Word client.
if (Office.context.requirements.isSetSupported('WordApi', '1.1')) {
// Implement functionality specific to the newer APIs.
addEventListener('keydown', insertEmersonQuoteAtSelection);
$(document).keypress(insertEmersonQuoteAtSelection);
$('#emerson').click(insertEmersonQuoteAtSelection);
$('#checkhov').click(insertChekhovQuoteAtTheBeginning);
$('#proverb').click(insertChineseProverbAtTheEnd);
$('#supportedVersion').html('This code is using Word 2016 or later.');
}
else {
// Informing you that this code will not be compatible with your Word version.
$('#supportedVersion').html('This code requires Word 2016 or later.');
}
});
$(document).ready(function () {
$(document).keypress(function (e) {
if (e.keyCode == 39) {
insertEmersonQuoteAtSelection;
}
});
});
});
I proceeded to test a specific key (e.g. 39) by adding another $(document).ready(function () { block, yet unfortunately, it did not respond as intended.
My objective was simply to confirm if executing a known working function like insertEmersonQuoteAtSelection upon a keypress would trigger successfully, but no execution took place.