Results 1 to 3 of 3

Thread: [2005] Office Garbage Disposal

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    [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:
    1. Dim wdApp As New Word.Application
    2.         Dim xlApp As New Excel.Application
    3.  
    4.         'Show Word
    5.         wdApp.Visible = True
    6.  
    7.         'Open new document based on template
    8.         wdApp.Documents.Add("X:\temp.dot")
    9.  
    10.         'Fill out address
    11.         wdApp.ActiveDocument.Bookmarks.Item("Address").Select()
    12.         wdApp.Selection.TypeText(Text:=strName)
    13.         wdApp.Selection.TypeParagraph()
    14.  
    15.         wdApp.Selection.TypeText(Text:=strCompany)
    16.         wdApp.Selection.TypeParagraph()
    17.  
    18.         'Release reference to MS Word
    19.         wdApp = Nothing
    20.  
    21.         'Show Excel
    22.         xlApp.Visible = True
    23.  
    24.         'Open billing log
    25.         xlApp.Workbooks.Open(Filename:=sheet.xls")
    26.  
    27.         xlApp.ActiveCell.Offset(0, 1).Select()
    28.         xlApp.ActiveCell.FormulaR1C1 = DateTime.Today
    29.  
    30.         'Release reference to MS Excel
    31.         xlApp = Nothing
    Is there a better way to interface with Office applications?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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:
    1. Dim wdApp As Word.Application
    2. Dim xlApp As Excel.Application
    3.  
    4. wdApp = DirectCast(CreateObject("Word.Application"), Word.Application)
    5. 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:
    1. 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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width