Length, Heigth and Width drawing views

Hello, 

 

I am trying to build a macro that can add general dimensions (length, heigth and width) to our 3 drawing views. My current approach works well for dimensioning the longest edge of a view (typically width or length), but it fails to consistently capture the overall height in certain orientations (especially for the front or right views, depending on geometry). This is because the longest edge in the drawing view doesn't always correspond to the height.

 

Here is the macro I am using : 

 

Any tips, help or guidance will be very appreciated. Thanks