Application not responding when opening Word file in asp.net?

I want to open a single docx file and then convert it to a pdf file in asp.net using the Microsoft.Office.Interop.Word package.

This is my code written in asp button click event ...

object fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF; Object missing = Type.Missing; object saveName = strURL.Replace(".docx", ".pdf"); object openName = docPath + "\\T4.docx"; Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = wdApp.Documents.Open(openName,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing); doc.SaveAs(saveName,fileFormat,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing); doc.Close(ref missing, ref missing, ref missing); 

But when running wdApp.Documents.Open () , problems arise.

A browser symbol is like loading always ...

I do not know what is the reason for the cause of this error ... please help me ...

+4
source share
2 answers

Microsoft does not support the automation of Office applications in a server environment, and this KB article explains some potential problems that you might encounter if you try.

I suggest you look for a third-party component like Aspose.

+2
source

This can happen when Word tries to display a dialog to the user. Interop is not smart enough to suppress all of these dialogs. Try the following:

1) Log in to the server and open MS Word manually. There may be some kind of dialog that requires user confirmation (for example, the licensing dialog box that appears when you first start Word). Once you go through these dialogs manually, they will no longer be a problem for interaction. (Also try opening one of these documents. Perhaps the problem is with the documents themselves.)

2) Suppress as many dialogs in your code as possible. I know 2 such places (DisplayAlerts and NoEncodingDialog).

 var word = new Word.Application(); word.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone; Word.Documents documents = word.Documents; documents.Open(openName, NoEncodingDialog: true); 

Note, but very important: to avoid memory leaks, you need to be very careful about how you reference and position these COM objects. Follow the tips in this thread . If you do, you can ignore skeptics who say that you should not use ASP.NET interaction. (For many years, I had a server that does exactly what you are trying to do - using Word interop to convert .docx to .pdf - and it works fantastically even under heavy load, but only because I followed the advice in this thread.)

+1
source

Source: https://habr.com/ru/post/1447766/


All Articles