Word add-ins can be developed using technologies like Office JavaScript API or VSTO (Visual Studio Tools for Office).
Below is an example using Office JavaScript API to get you started
// JavaScript code for Office JavaScript API
Office.onReady((info) => {
if (info.host === Office.HostType.Word) {
// Add a custom button to the ribbon when the add-in is activated
const ribbonButtons = Office.Ribbon.ButtonType;
Office.ribbon.requestUpdate({
tabs: [
{
id: "myTab",
label: "My Tab",
groups: [
{
id: "myGroup",
label: "My Group",
buttons: [
{
id: "sendButton",
label: "Send Document to Server",
type: ribbonButtons.Button,
iconUrl: "https://example.com/icon.png",
enabled: true,
onClick: sendDocumentToServer,
},
],
},
],
},
],
});
}
});
async function sendDocumentToServer(eventArgs) {
try {
// Get the current document
await Word.run(async (context) => {
const body = context.document.body;
// Load the body content to access its text
body.load("text");
await context.sync();
// Send the document content to the server using a POST request
const serverUrl = "https://example.com/upload";
const content = body.text;
const requestOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ content }),
};
const response = await fetch(serverUrl, requestOptions);
if (response.ok) {
console.log("Document successfully sent to the server.");
} else {
console.error("Failed to send the document to the server.");
}
});
} catch (error) {
console.error("Error occurred: " + error);
}
}