For a VBA program that uses a single object variable multiple times, is it best to reset the variable in the loop without first setting the variable to nothing (which can be done at the end outside of the loop), or should the variable always be set to nothing after each instance of use. From a reference counting perspective it seems best to set to nothing as I never have need for the previous state of the object once it has been changed to the next variable (I'm not sure how one would retrieve the previous state but I gather that there must be a function of such from this question). Another relevant question that I examined can be found here.
The application which triggered this question is one where I cycle through many Word documents, pulling relevant information from each of them. See example below:
dim objDoc as object, objWord as object
Set objWord = CreateObject("Word.Application")
for r=2 to 40
'Some code to set filename
Set objDoc = objWord.Documents.Open(filename, ReadOnly:=False, Visible:=False)
'Code to retrieve relevant info from document
objDoc.close
Set objDoc = nothing 'Should this go here?
next r
set objDoc = nothing 'Or should it go here?
objWord.Quit
The Question: Does it matter when you set an object variable to nothing in terms of code efficiency or problem avoidance. I have highlighted above two options for where I believe that should go with the third option being that it doesn't matter when I set the variable to nothing