Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I got some codes here which is generating a word file. It is working fine in my local host and in the test server. The winword.exe is properly closing and releasing the memory. However, upon testing it to the another server, the winword.exe still remain in background process and memory is not released even after the generation.
I monitored the CPU usage of the server and it's not even high and goes back to 0% after word file generation but the winword.exe remains in background.
I am new to this. Please help. Thanks!

What I have tried:

Public Sub Generate_Word(ByVal strResult As List(Of String))
Dim strTemplateFile As String = "~\TestFolder\TestFile.doc"
Dim WordApp As New Word.Application
Dim newDoc As Word.Document
newDoc = WordApp.Documents.Add(HttpContext.Current.Server.MapPath(strTemplateFile))

'*** Generate word file here ***'

'*** Save the file here***'

ReleaseObject(rng)
newDoc.Close(False)
ReleaseObject(newDoc)

WordApp.Application.Quit(False)
ReleaseObject(WordApp)

'*********** Start of clean up code *************'
GC.Collect()
GC.WaitForPendingFinalizers()

'GC needs to be called twice in order to get the Finalizers called
'the first time in, it simply makes a list of what is to be
'finalized, the second time in, it actually is finalizing. Only
'then will the object do its automatic ReleaseComObject.

GC.Collect()
GC.WaitForPendingFinalizers()
'*********** End of clean up code *************'

'*** save the file to database here ***'
End Sub

Public Sub ReleaseObject(ByVal obj As Object)
Try
Dim intRel As Integer = 0
Do
intRel = System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
Loop While intRel > 0
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally

End Try
End Sub
Posted
Updated 24-May-17 23:07pm
Comments
Richard Deeming 25-May-17 14:34pm    
Considerations for server-side Automation of Office[^]
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

DocX[^] is free, and doesn't suffer from the same limitations.
Aifos Lavigne 26-May-17 1:09am    
Hi, I'm going to try it. Thanks!

1 solution

Automating Word (or any Office app) is not supported under asp.net, Microsoft have never said this is going to work, they have said the opposite. Use something else to make your documents that is supported under asp.net such as DocX, Office XML SDK, Aspose and so on.
 
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