[2005] Office Garbage Disposal
I don't quite understand the literature I have read on garbage disposal.
Is it OK to do what I have done here or do I have to call the Dispose() method?
Whats the difference?
I added a reference to the COM Object Microsoft Word 9 Object Library (and the Excel equivalent).
VB Code:
Dim wdApp As New Word.Application
Dim xlApp As New Excel.Application
'Show Word
wdApp.Visible = True
'Open new document based on template
wdApp.Documents.Add("X:\temp.dot")
'Fill out address
wdApp.ActiveDocument.Bookmarks.Item("Address").Select()
wdApp.Selection.TypeText(Text:=strName)
wdApp.Selection.TypeParagraph()
wdApp.Selection.TypeText(Text:=strCompany)
wdApp.Selection.TypeParagraph()
'Release reference to MS Word
wdApp = Nothing
'Show Excel
xlApp.Visible = True
'Open billing log
xlApp.Workbooks.Open(Filename:=sheet.xls")
xlApp.ActiveCell.Offset(0, 1).Select()
xlApp.ActiveCell.FormulaR1C1 = DateTime.Today
'Release reference to MS Excel
xlApp = Nothing
Is there a better way to interface with Office applications?
Re: [2005] Office Garbage Disposal
Setting a variable to Nothing means that that variable no longer refers to that object. If no other variables refer to that object either then that object is flagged as eligible for garbage collection. Calling Dispose on an object explicitly tells that object to release any unmanaged resources it is holding and to also call the Dispose method of any managed objects it is holding so that they may do the same.
You create an object and assign it to a variable. If you then set that variable to Nothing it simply means that that variable no longer refers to that object, but the object is still there. What happens if you do that is that when the garbage collector comes along is it sees that the object has not been destroyed so it will take at least one pass to destroy it, then it will not reclaim the memory it occupies until a later pass. If you Dispose it yourself then the memory will be reclaimed the first time the garbage collector comes around.
The rules of memory management in .NET are very simple. If an object has a Dispsoe method the ALWAYS call it when you're finished with the object. After that, you should set a reference-type variable to Nothing if you're finished with it and it will not or may not lose scope for some time. Setting value-type variables to Nothing is pointless. Likewise setting reference-type variables that soon lose scope to Nothing is also pointless.
Re: [2005] Office Garbage Disposal
I wouldnt use the New keyword in the declaration line of code. always better to use it like so.
VB Code:
Dim wdApp As Word.Application
Dim xlApp As Excel.Application
wdApp = DirectCast(CreateObject("Word.Application"), Word.Application)
xlApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
And instead of "select"ing the bookmark you should use its Range property to insert your text.
VB Code:
wdApp.ActiveDocument.Bookmarks.Item("Address").Range.Text = strName
Also, I wouldnt use the ActiveCell property as its better to reference to exact cell you wish to modify.
Then it looks like your leaving the apps open? If not, then closing and destroying the objects would be better.