Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have an application which generates a report using Word file template. The application will fill up the template with the details supplied by the users and database records. I'm running my application to another server using Remote Desktop.

My problem is that the WINWORD.EXE process fails to terminate and remains in the task manager after the report generation is completed (saved the report to database and delete the generated file).

When the Service Account is signed-in, the WINWORD.EXE process is terminating properly and is being removed in the task manager’s list. When sign-off, the WINWORD.EXE remains in the list. And this may lead to Out-of-Memory-Exception when several Word processes are not terminated.

Word reports are generated using Microsoft.Office.Interop.Word, Version=12.0.0.0

Here's the code in saving the new file, closing the Word application, saving the report, and deleting the generated file:

VB
Marshal.releaseComObject(rng)

Dim strFileName As String = comm.GenerateFileName(strUser_id, strVPN) & ".docx"

' Save the document.
Dim filename As Object = Path.GetFullPath(strNewFilePath & strFileName)
newDoc.SaveAs(FileName:=filename)

' Close.
Dim save_changes As Object = False
newDoc.Close(save_changes)
WordApp.Quit(save_changes)

Marshal.ReleaseComObject(newDoc)
Marshal.ReleaseComObject(WordApp)

rng= Nothing
newDoc = Nothing
WordApp = Nothing

' Let GC know about it
GC.Collect()

' Save the file to database
SaveFormat(Filename, strUser_Id, strFamilyId, strVPN, DateTime.Now.ToString(), "application/vnd.ms-word", br_id, "doc")

If File.Exists(filename) then
      File.Delete(filename)
End If


What I have tried:

I'm still researching on different work around here.

In excel we get the solution by getting the process ID of the opened Excel instance using the Hwnd property and GetprocessById class.

I read somewhere that Word 2013 and later has an Application.Hwnd property. Is it true? Does Word have hwnd property?

Your help is much appreciated. Thanks :)
Posted
Updated 16-Apr-17 19:22pm
v2

1 solution

You cannot use Office Interop in an ASP.NET application. Why? Multiple requests at the same time are not supported. In other words, Office apps don't support reentrancy.

You can read more about it here[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900