Welcome to these Forums!
I don't want to sound discouraging, but that seems like an almost impossibly complicated thing to get a routine to do. I suspect that, in the time it would take someone to come up with the code to do it automatically, you could do very many such outlines manually. Even if you are able to write the code yourself, it would not be a time-saver overall unless you do a lot of these.
One of the main difficulties is that such a routine would be required to compare virtually every point on every object with every other other object in the selection [or perhaps in the entire drawing?]. If those white rectangles might ever be made up of Lines rather than as single Polylines, it gets even more complex -- for example, how is it to decide which Lines it can ignore, and which are relevant to the result? Other complications: Might the outlying pieces ever not be closed? Would there ever be curves involved?
Getting it to recognize the correct conditions requiring closure would be very difficult. Here's a slightly altered approximation of your image [I moved the gap along the bottom over in relation to what's above it, assuming that kind of relationship is possible]. It's hard to imagine how to get it to not "find" the magenta/pink elements as closure opportunities, instead of the closures in your image. Also, I suspect the red ones at the top left would be hugely simpler [though not "simple"] for it to recognize and calculate than the vertical ones in your image.

I suggest you Search for threads [in the "Community," not just "this Board;" there was one somewhere pretty recently] involving "Boundary," that talk about drawing a Circle or closed Polyline or something around the entire thing, and using Hatch as a start, because [in new-enough versions] it can find and identify certain kinds of gaps. It won't do the whole thing for you, but it may give you a quicker start than manually closing all the gaps, etc.
Kent Cooper, AIA