4

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Saravanan
  • 11,372
  • 43
  • 143
  • 213
  • First, you don't need the `missing, missing...`. You can use named parameters instead, and simply leave out any param that would be missing. Second - what is the error you're getting? – Anders Arpi Nov 22 '12 at 15:28
  • @AndersHolmström : there is no error...browser is loading loading...i already put a break point but it is staying on the above mentioned line...it did not come after that line... – Saravanan Nov 22 '12 at 15:31
  • Do you have Office installed on the machine where this is running? – Anders Arpi Nov 22 '12 at 15:32
  • @AndersHolmström : Yes, Office 2007 installed on the machine... – Saravanan Nov 22 '12 at 15:35
  • Have you tried stepping through the code? Does it really just 'stop' at the `wdApp.Documents.Open(...)`? It doesn't crash or give an error? Seems weird. – Anders Arpi Nov 22 '12 at 15:36
  • @AndersHolmström : yes it is true...right now itself it keeps running and not throwing any error... – Saravanan Nov 22 '12 at 15:37
  • Try setting `wdApp.Visible = true` before opening the file and see what is going on. – Anders Arpi Nov 22 '12 at 15:39
  • @AndersHolmström : I already did stepping through each and every line... Actually it is not stopped on that line.When it comes to that line, the yellow mark is gone and it suddenly turned to browser.And the browser keeps loading the connection symbol always(connecting...) – Saravanan Nov 22 '12 at 15:43
  • @AndersHolmström :Ok i will try and let you know – Saravanan Nov 22 '12 at 15:43
  • @AndersHolmström :Sorry AndersHolmström. no improvement.same problem this time also(after putting wdApp.Visible = true) – Saravanan Nov 22 '12 at 15:53
  • What's happening with the word application then? It should be visible on the server. – Anders Arpi Nov 22 '12 at 15:54
  • @AndersHolmström : No... It is not visible on the server machine... – Saravanan Nov 22 '12 at 15:56
  • Well, I'm past being able to help you here. Too many unknown factors that would need checking out. Hopefully somebody else knows of this problem. – Anders Arpi Nov 22 '12 at 15:57
  • Also I would like to add that starting an instance of Word on a web server when the user clicks a button is a really bad practice that can lead to trouble. Are you sure you need to do it this way? – Anders Arpi Nov 22 '12 at 15:58
  • @AndersHolmström : My Requirement is I want to convert specified docx file into pdf file...Can we do it on some other way using Microsoft.Office.Interop.Word package – Saravanan Nov 22 '12 at 16:00
  • I think you need to do this in some other way than an ASP environment. Check this question: http://stackoverflow.com/questions/9438577/using-microsoft-office-interop-word-in-asp-net – Anders Arpi Nov 22 '12 at 16:01
  • @AndersHolmström : When i run my web application using VS-2010 deployment server then the docx file is converted to pdf file successfully.I got expected output.But now i have configured the same application in IIS(5.1) then it not working...and keeps the status that i already said above... Do i need to change any settings on IIS 5.1? Can you help me with this... – Saravanan Nov 23 '12 at 09:50
  • You should not try to do this with Office interop. Sorry. You will be wasting your time. See the answer Joe wrote a while ago, that's probably the best solution. – Anders Arpi Nov 23 '12 at 09:51
  • @Saravanan Did you manage to solve it without using a third party? – Wessam El Mahdy Jan 14 '17 at 13:23

2 Answers2

2

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.

Joe
  • 122,218
  • 32
  • 205
  • 338
2

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.)

Keith
  • 20,636
  • 11
  • 84
  • 125
  • Mostly good advice, but: "... you can ignore the naysayers who say you shouldn't use interop with ASP.NET" - don't ignore the naysayers. Instead, read the KB article I posted, understand the implications of what you're doing, and decide whether in your environment it's a better solution than the alternatives (such as 3rd party components like Aspose). – Joe Nov 29 '12 at 18:41
  • @Keith could you please give me a link to your website, I'm trying to do something similar and I'm anxious from using Ms office. BTW: I'm still having the same problem as Joe – Wessam El Mahdy Jan 14 '17 at 13:20
  • @Joe Did you manage to solve your problem? I'm having the same situation here that drive me nuts :) please advise. – Wessam El Mahdy Jan 14 '17 at 13:22
  • The link is dead. – ninjaxelite Dec 09 '19 at 11:56