Attachment Sequence Order VBA

Dear VBA experts,

I am trying to create a routine that will batch process files to manipulate the sequence order of attachments in a design model file. I am trying to move the ActiveDesignFile to the end of the order. This in general is achieved by clicking Settings > Update Sequence,  in Reference Dialog box.

The following codes has some problems due to which the ActiveDesignFile doesn't move all the way to the bottom if the sequence order of attachments is not incremental. i.e. 1,2,3,4 and so on. For example if I update the sequence in the order of 1,4,3,2 this routine with move the ActiveDesignFile below the attachment order of 4.

The codes traces just fine except the final results don't come as expected. Your help would help me debug and understand the situation. Here is my code. The attached file is the mbva file of the same. You can simple rename it to remove the "zip" extension from this file.

Option Explicit
Sub UpdateSequence_ActiveModel_ToEnd()

'MicroStation related variables
Dim attachmentsCount As Long
attachmentsCount = ActiveModelReference.Attachments.Count

Dim expectedSum As Long
Dim currentSum As Long
Dim missingNumber As Long

'Assign currentSum to Zero to begin with
currentSum = 0

Dim i As Integer
Dim tempArray() As Variant
ReDim tempArray(attachmentsCount - 1)

'Check if the model has attachments if not exit sub
If (ActiveModelReference.Attachments.Count <> 0) Then
'If the model has only one attachment then change the updateorder to 0 immediately and exit
If (ActiveModelReference.Attachments.Count = 1) Then
ActiveModelReference.Attachments(1).UpdateOrder = 0
'Save Settings so that the update order is saved.
SaveSettings
'Redraw all views to reflect changes
RedrawAllViews
Exit Sub
Else
'Find the expectedSum to compare against
expectedSum = (attachmentsCount + 1) * (attachmentsCount / 2)
'Loop though each attachments item
For i = 1 To attachmentsCount
'Populate a temporary array with the current update orders
tempArray(i - 1) = ActiveModelReference.Attachments(i).UpdateOrder
currentSum = tempArray(i - 1) + currentSum
Next i
'Find the missing number
missingNumber = expectedSum - currentSum
'MsgBox ("Missing Number is: " & missingNumber)
'Update the rest of the attachments to bring current model to the highest order
If (missingNumber = attachmentsCount) Then
'The active design file is the last in the order. Thus exit.
SaveSettings
RedrawAllViews
CadInputQueue.SendCommand "Dialog -127"
Exit Sub
Else
For i = 1 To attachmentsCount
If (tempArray(i - 1) > missingNumber) Then
ActiveModelReference.Attachments(i).UpdateOrder = tempArray(i - 1) - 1
'MsgBox tempArray(i - 1)
'SaveSettings
'Update the views
'MsgBox ("Reference name is: " & ActiveModelReference.Attachments(i).AttachName & " & UpdateOrder is: " & ActiveModelReference.Attachments(i).UpdateOrder)
End If
SaveSettings
Next i

End If
End If

Else
Exit Sub
End If
'SaveSettings
RedrawAllViews
'MsgBox ("Reference name is: " & ActiveModelReference.Attachments(i).AttachName & " & UpdateOrder is: " & ActiveModelReference.Attachments(i).UpdateOrder)
CadInputQueue.SendCommand "Dialog -127"


End Sub
updateSequence01.mvba.zip