By the term "behind" and the close window step you show in your script, I conclude that you have one window open to a Blends layout and one window open in front of it open to components.
What you need to do is capture the current blend number in a global variable before you open the components window.
IF you are using a script to open that window, include this step before that script changes layouts:
Set Variable [$BlendID ; Blend::BlendNo ]
Then the script above can use this set field step to link the component record to the currently selected Blend:
Set Field [ Component::BlendNo ; $BlendID ]
Many thanks Phil, I should be able to do that!
Ps I'm very proud of myself.... I seem to be making progress with my understanding.
Ummm but I was suffering from a caffeine deficiency when I posted that. It needs to be a global variable so use two dollar signs instead of one.
I understand (and understood at the time). I was suffering sleep deficiency, so you may have noticed I ticked my own answer instead of yours...