Macro for Moving Drawing Views

Hello everyone,

 

I'm trying to write a macro that organizes my drawing views on a single sheet. Essentially, I have 20-40 views on a sheet but when I add them they are randomly placed. So I'm trying to create a macro that will look at my sheet size, look at the view sizes, and move them into rows and columns to create a cleaner sheet. I have a macro halfway built but for some reason it only stacks my parts vertically. If anyone can help, I'd really appreciate it! Attached are a few images, one shows what the code gives me and the other shows roughly what I'm hoping to accomplish. Thanks again! Here is the code, 

Dim swApp As Object
Dim swModel As ModelDoc2
Dim swDrawing As DrawingDoc
Dim swSheet As Sheet
Dim swView As View
Dim viewList As Collection

Sub main()
   Set swApp = Application.SldWorks
   Set swModel = swApp.ActiveDoc

   ' Ensure the active document is a drawing
   If swModel.GetType <> swDocDRAWING Then
       MsgBox "This macro only works with drawing documents.", vbExclamation, "Error"
       Exit Sub
   End If

   Set swDrawing = swModel
   Set swSheet = swDrawing.GetCurrentSheet

   Dim sheetWidth As Double
   Dim sheetHeight As Double
   Dim xSpacing As Double
   Dim ySpacing As Double
   Dim xOffset As Double
   Dim yOffset As Double

   ' Get the size of the sheet
   swSheet.GetSize sheetWidth, sheetHeight

   ' Get the size of the sheet
   swSheet.GetSize sheetWidth, sheetHeight

   ' Show the sheet size in Immediate Window (Ctrl+G to view it in the VBA editor)
   Debug.Print "Sheet Width: " & sheetWidth & " meters"
   Debug.Print "Sheet Height: " & sheetHeight & " meters"

   ' Or use a MsgBox if you prefer a pop-up
   MsgBox "Sheet Width: " & sheetWidth & " m" & vbCrLf & "Sheet Height: " & sheetHeight & " m", vbInformation, "Sheet Dimensions"


   ' Define spacing between views
   xSpacing = sheetWidth / 10
   ySpacing = sheetHeight / 10

   ' Initial offsets
   xOffset = xSpacing
   yOffset = ySpacing

   ' Collect all views
   Set viewList = New Collection
   Set swView = swDrawing.GetFirstView

   ' Skip the sheet format view
   If Not swView Is Nothing Then
       Set swView = swView.GetNextView
   End If

   ' Add views to collection
   While Not swView Is Nothing
       viewList.Add swView
       Set swView = swView.GetNextView
   Wend

   ' Arrange views
   Dim row As Integer
   Dim column As Integer
   row = 0
   column = 0

   For Each swView In viewList
       Dim posX As Double
       Dim posY As Double

       Dim viewBounds As Variant
       viewBounds = swView.GetOutline() ' Returns an array of minX, minY, maxX, maxY

       Dim viewWidth As Double
       Dim viewHeight As Double
       viewWidth = viewBounds(2) - viewBounds(0)
       viewHeight = viewBounds(3) - viewBounds(1)

       posX = xOffset + (column * xSpacing)
       posY = sheetHeight - yOffset - (row * ySpacing)

       ' Adjust position to keep views on the sheet
       If posX + viewWidth > sheetWidth Then
           column = 0
           row = row + 1
           posX = xOffset
       End If

       If posY - viewHeight < 0 Then
           MsgBox "Not enough space on the sheet to arrange all views.", vbExclamation, "Warning"
           Exit Sub
       End If

       If posX + viewWidth > sheetWidth Then
           posX = sheetWidth - viewWidth - xOffset
       End If

       If posY - viewHeight < 0 Then
           posY = viewHeight + yOffset
       End If

       swView.Position = Array(posX, posY)

       column = column + 1
   Next swView


   MsgBox "Views have been arranged successfully!", vbInformation, "Done"
End Sub