Align multiple objects edge to edge

This is the closest I could find: https://community.coreldraw.com/talk/coreldraw_graphics_suite_x5/f/coreldraw-graphics-suite-x5/25957/another-free-but-less-exciting-macro/121403#121403

But it aligns only 2 objects. And I cannot edit the macro

I need to align multiple objects, either horizontally or vertically (not all 16 variants of the above macro)

Something like attached picture (top row = before, bottom row = after).

Thanks.