1

I am using compareDocuments function to compare two documents and returning compared document as a byte array as response to front end , I don't know how to convert this byte array and open it as word document on my desktop word after calling my API on taskpane Add-in without downloading the file to my system. I want two show the compared doc on word Desktop.

Below is my C# code for the API which I created: -

[HttpPost]
        [Route("compare")]
        public async Task<object> compare(string original, string revised)
        {
            Word.Application wordApp = new Word.Application();
            wordApp.Visible = false;
            object wordTrue = (object)true;
            object wordFalse = (object)false;
            object fileToOpen = @original;
            object missing = Type.Missing;
            Word.Document doc1 = wordApp.Documents.Open(ref fileToOpen,
                   ref missing, ref wordFalse, ref wordFalse, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref wordTrue, ref missing,
                   ref missing, ref missing, ref missing);

            object fileToOpen1 = @revised;
            Word.Document doc2 = wordApp.Documents.Open(ref fileToOpen1,
                   ref missing, ref wordFalse, ref wordFalse, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
                   ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing);

            Word.Document doc = wordApp.CompareDocuments(doc1, doc2, Word.WdCompareDestination.wdCompareDestinationNew,
                                Word.WdGranularity.wdGranularityWordLevel,
                                true, true, true, true, true, true, true, true, true, true, "", true);

            doc1.Close(ref missing, ref missing, ref missing);
            doc2.Close(ref missing, ref missing, ref missing);

            // Hides both original and revised documents
            wordApp.ActiveWindow.ShowSourceDocuments = WdShowSourceDocuments.wdShowSourceDocumentsNone;

            wordApp.Visible = false;
            //doc.Activate();
            object filePath = @"C:\Users\yatin\OneDrive\Documents\compared_document.docx";
            doc.SaveAs(ref filePath);
            doc.Close();
            wordApp.Quit();

            byte[] byteArray;
            using (FileStream fs = new FileStream(filePath.ToString(), FileMode.Open, FileAccess.Read))
            {
                byteArray = new byte[fs.Length];
                fs.Read(byteArray, 0, (int)fs.Length);
            }

            
            // Return the byte array to the frontend
            return byteArray;

        }

I want to use this API with my frontend ADD-IN and show the compared document on the frontend device without downloading it, everytime I run my code the compared document opens up on server side ,so I tried converting the compared doc to byte array and send it as a response to frontend, but I cannot open the word doc on desktop in frontend. Is there any way we can solve this issue?

2 Answers 2

1

First of all, automating MS Word from any web server is not really a good idea.

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.

It seems you need to received the byte array and then save it as a file on the client side, see How to load Word document from byte array for more information. But I don't think any JS code can access the local filesystem for security reasons. The best what your application could do is to ask users to download the generated file. Or just open the file in Office365 editor prior uploading the generated file to the OneDrive server.

Sign up to request clarification or add additional context in comments.

1 Comment

I wanted to compare two word documents using word Add-ins, later I figured out that there was no such function in Office.js to compare two word documents and the only way to do that was using c# or VB, so I figured out a way to do this server side and send it as a response to frontend and open the compared word document their directly, but I am unable to do that, do you know any alternative so that I can create my add-in
0

So instead returning compared document as a byte array as response to frontend what I did was I uploaded the compared document to a temporary folder on sharepoint and then returned it's webDavUrl as response to frontend for opening the file and it worked for me.

1 Comment

Posting and marking your own answers doesn't make any benefits. If you need to share the end solution consider using comments instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.