I have an application which generates a report using a Word file template. The application will fill up the template with the details supplied by the user's 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 signing off, the WINWORD.EXE remains in the list. This may lead to an 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:
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
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 a HWND property?