Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net C# - WINWORD.exe

I am playing with Microsoft word document from ASP.NET/C# program.

My programs opens up word docs internally. using

app = new Word.Application();
app.Documents.Open

it creates a instance of winword.exe process, I can see it in Task Manager.

even if I closes Doc using close() and app.quit(). it should kill the process. but this process doesnt get killed.

any idea how to kill this process programmatically.

like image 547
sandip Avatar asked Oct 20 '25 03:10

sandip


1 Answers

To begin, I agree with Arthur and Ivo: don't use Word automation on the server... it's not supported and it's painful.

  1. If you can, try supporting Word input/output via the DOCX format (fully documented XML format packaged using compression that's supported by the .NET Framework and supported by pre-Office 2007 with a plugin)
  2. If not create a service that handles the requests in a queue to prevent multiple threads using it at the same time (ala Ivo's answer)

If you must use Word automation:

  1. Make sure you are setting as many Application options as possible to prevent dialogs
    • Options.ConfirmConversions = false
    • DisplayAlerts = wdAlertsNone
    • FeatureInstall = msoFeatureInstallNone
  2. Make sure you are passing in WdSaveOptions.wdDoNotSaveChanges to Document.Close
  3. Have a look through this document: How To: Dismiss a Dialog Box Displayed by an Office Application with Visual Basic .NET (also applies to C#, obviously)
  4. Make sure you call Marshal.ReleaseComObject on both the Document and Application objects, and make sure it's in a finally block.

The winword.exe should go away if you follow the above, you should not need to kill the process.

Edit: Not really an answer to your question, but on topic nonetheless. Keep in mind that you're also going to have problems on the server with permissions. Make sure you create a specific user for it and use that user as your AppPool identity (or service identity). You'll then need to do the following:

  1. Make sure you've installed the automation components, obviously
  2. Run "dcomcnfg"
  3. Component Services -> Computers -> My Computers -> COM+ Applications
  4. "Microsoft Word document" (or "{000209FF-0000-0000-C000-000000000046}") -> Properties
  5. Security tab -> Launch and Activation permissions -> Customize
  6. Click "Edit" and add your AppPool user from above
  7. Repeat steps 5-6 but select "Access Permissions"

On top of all that, you still might need to run winword.exe once as your AppPool user directly from the server, despite the dialog options above, to initially setup the profile.

like image 118
Richard Szalay Avatar answered Oct 21 '25 17:10

Richard Szalay