Issues with Text Manipulation Visibility when Importing External DOCX in MS Word Add-in using JavaScript API

45 Views Asked by At

I am developing a Microsoft Word add-in using the JavaScript API, and I am encountering a problem when trying to import an external DOCX file into the current document. To achieve this, I converted the DOCX file to a Base64 format and used the following function:

const openExternalDocument = async () => {
    await Word.run(async (context) => {
        console.log("Opening external document...");
        // Use the Base64-encoded string representation of the selected .docx file.
        const externalDoc = context.application.createDocument(getDocumentAsBase64());
        await context.sync();

        const externalDocBody = externalDoc.body;
        externalDocBody.load("text");
        await context.sync();

        // Insert the external document's text at the beginning of the current document's body.
        const externalDocBodyText = externalDocBody.text;
        const currentDocBody = context.document.body;
        currentDocBody.insertText(externalDocBodyText, Word.InsertLocation.start);
        await context.sync();
    }).catch(function (myError) {
        console.log("Error", myError);
    });
};

While the document is successfully imported, I am facing an issue where all the text manipulation done in the external document is not visible in the current document. Any insights or suggestions on why this might be happening and how I can ensure the visibility of text manipulations from the external document in my Word add-in?

orginal document:

orginal document

[imported document:

imported document

1

There are 1 best solutions below

0
On

You didn't need to get text from body of externalDoc when you get only text of this document and insert in your document text wasn't pick style.

try this this will work 100%: insertfilefrombase64

const openExternalDocument = async () => {
  await Word.run(async (context) => {
    const currentDocBody = context.document.body;
    const externalDoc = DocumentBase64;
    currentDocBody .insertFileFromBase64(base64File,"Start")
    await context.sync();
 }).catch(function (myError) {
    console.log("Error", myError);
 });
};