Saravanan
Saravanan

Reputation: 11592

Application not responding when opening a Word File in asp.net

I want to open one docx file and then convert it into pdf file in asp.net using 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 it has some trouble on wdApp.Documents.Open() line when executing.

Browser symbol seems like loading always.

I don't know what is the cause of reason for this error.

Upvotes: 4

Views: 2796

Answers (2)

Keith
Keith

Reputation: 21224

This can happen when Word is trying to display a dialog to the user. The interop isn't 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 sort of dialog that needs user confirmation (such as the licensing dialog that displays the first time you run Word). Once you get past these dialogs manually they'll stop being a problem for the interop. (Also try opening one of those documents. Perhaps the issue is with the documents themselves.)

2) Suppress as many dialogs as possible in your code. I'm aware of 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);

A side note, but very important: To avoid memory leaks you need to be very careful about how you reference and dispose these COM objects. Follow the advice in this answer. If you do, you can likely use interop successfully with ASP.NET. (For years I've had a server that's doing exactly what you're trying to do -- using Word interop to convert .docx to .pdf -- and it works fantastically even under heavy load, but only because I properly dispose of every COM object reference.)

Upvotes: 2

to StackOverflow
to StackOverflow

Reputation: 124686

Microsoft doesn't support automation of Office applications in a server environment and this KB article explains some of the potential problems that can occur if you try it.

I suggest you look for a third-party component, such as Aspose.

Upvotes: 2

Related Questions