destroying local objects

24 views
Skip to first unread message

gertja...@gmail.com

unread,
Sep 1, 2026, 7:13:43 AMSep 1
to Excel-DNA
Hello all.   Sorry if this is a bit basic, but when working with Excel-DNA in VB.NET, is it recommended to "destroy" locally declared objects like worksheets or ranges when finished with them and what's the best way to do so?  I used to "set equal to Nothing" in days gone by.

Govert van Drimmelen

unread,
Sep 1, 2026, 8:02:39 AMSep 1
to exce...@googlegroups.com

Hi Gert-Jan,

 

No, you don’t have to do anything like that. The .NET runtime will track all the COM references and clean them up as part of the garbage collection, if they have no more references to them.
See my post here: https://stackoverflow.com/a/38111137/44264 and similar.

The important rule, though, is to only ever talk to the Excel COM object model from the main thread (test that System.Threading.Thread.ManagedThreadId = 1).
That means you have to be careful with callbacks from timers, network, async and such cases.
You should never call the COM object model from another thread, but can set up work to be done on the main thread by calling ExcelAsyncUtil.QueueAsMacro.

-Govert

--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/exceldna/5741df0d-f102-4ae5-9f36-bafa67c7a9dfn%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages