By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,402 Members | 1,695 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,402 IT Pros & Developers. It's quick & easy.

Wrapping Word.Application object with an IDisposable class?

P: n/a

I'm using VB2005 + Office XP Enterprise.

If you create a Word.Application object, you risk a memory leak if your
application crashes because Word.Application is an unmanaged COM Interop

I want to encapsulate the Word.Application in a Class that implements
IDisposable to avoid this potential memory leak. Here is the code I have:

Imports Microsoft.Office.Interop
''' <summary>
''' Encapsulates MS Word using IDisposable to prevent memory leaks. Use
WordFactory.GetWordApp to obtain a memory-safe Word.Application object
''' </summary>
''' <remarks></remarks>
Public Class WordFactory
Implements IDisposable
Public Sub New()
mobjWordApp = New Word.Application
End Sub
Private mobjWordApp As Word.Application
Public ReadOnly Property WordApp() As Word.Application
Return mobjWordApp
End Get
End Property
Public Shared Function GetWordApp() As Word.Application
Dim wf As New WordFactory
Return wf.WordApp
End Function
#Region " IDisposable Support "
Private disposedValue As Boolean = False ' To detect
redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
'free managed resources when explicitly called
End If

'free unmanaged resources
On Error Resume Next
On Error GoTo 0
End If
Me.disposedValue = True
End Sub
' This code added by Visual Basic to correctly implement the
disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
' Do not change this code. Put cleanup code in Dispose(ByVal
disposing As Boolean) above.
End Sub
Protected Overrides Sub Finalize()
Call Dispose(False)
End Sub
#End Region
End Class

Now the odd thing is that it seems to work fine when compiled. Code such as

Dim w As Word.Application = WordFactory.GetWordApp
Throw New Exception("An unhandled exception was intentionally thrown
for testing purposes")

Finalizes the Word.Application object correctly (i.e. it disappears from
Task Manager), but if the program is run from the VB IDE, WINWORD.EXE stays
in Task Manager.

Is this some artefact of the debugger? Or am I doing something wrong?
Oct 10 '07 #1
Share this Question
Share on Google+
1 Reply

P: n/a

Would you mind letting me know the result of the suggestions? How is the
issue in your side? Is it a Windows form project or a console one? If you
need further assistance, feel free to let me know. I will be more than
happy to be of assistance.

Have a great day!

Jialiang Ge (ji****, remove 'online.')
Microsoft Online Community Support

When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 16 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.