I am curious to understand what the problem is.
Right now, I am working 400 km away from home and only have access to an old PC running Windows 10 and AutoCAD 2002.
I must say that this code actually works for me.
I will try testing it soon on a more recent version of AutoCAD, and then perhaps I’ll add a further comment.