1) Does End Sub "Destroy" all Objects created in the Sub? 2)How to "Destroy" an Object created via a "Fully Qualified Object Variable? Like... MsgBox RRS feed

  • Question

  • As I understand it using "Fully Qualified Object Variables" in your code... creates an object... For Instance...

    MsgBox Outlook.Application.GetNamespace("MAPI").Folders.Item("My Outlook folders").Folders.Item(X).Folders.Item(Y).Folders.Item(Z).Name

    To display the name of an Outlook folder 3-levels down.

    Question1: Is this Object destroyed when the Sub ends?

    Question2: Can you destroy an Object created like this with code like

    Set Bla.bla.bla.bla = Nothing

    Question3: If a Fully Qualified Object Variable like the this was used in a loop to display all the folder names in folder X\Y like maybe...

    X = 1: Y = 1

    Z=Outlook.Application.GetNamespace("MAPI").Folders.Item("My Outlook folders").Folders.Item(X).Folders.Item(Y).Folders.Count

    For c = 1 to Z
    MsgBox Outlook.Application.GetNamespace("MAPI").Folders.Item("My Outlook folders").Folders.Item(X).Folders.Item(Y).Folders.Item(Z).Name
    Next Z


    And maybe Z was equal to 5 then five Objects would be created by the above loop... is this correct?
    Thanks for any help.


    • Edited by Mel_3 Thursday, December 1, 2011 4:40 PM
    Thursday, December 1, 2011 4:37 PM


  • In VBA, to create an object variable pointing to an object, one is required to use the Set statement.
    Object references are automatically destroyed when the sub/function exits; except when using Automation.
    In that case you should explictly destroy all object references by setting them to nothing.

    Jim Cone
    Portland, Oregon USA
    (List Files XL add-in:  finds and lists files/folders with hyperlinks)

    • Edited by Jim Cone Saturday, December 3, 2011 7:29 PM
    • Proposed as answer by Spilly39 Monday, December 5, 2011 10:52 PM
    • Marked as answer by danishaniModerator Thursday, January 12, 2012 1:21 AM
    Saturday, December 3, 2011 7:28 PM