Word add-in to send current document to a webserver

STM0O0 1 Reputation point
2021-02-20T23:29:45.577+00:00

Hi

Is it possible to create a Word add-in that adds a button to Word to sends the current document (docx) to a certain web server (POST)?
If yes, does such an add-in already exists somewhere that I can use or adapt to work with my web server? Or a code example that can get me started?

Thanks in advance for your help!
STM0O0

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,509 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Vahid Ghafarpour 17,875 Reputation points
    2023-08-01T18:42:14.6333333+00:00

    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);
      }
    }
    
    
    0 comments No comments