Move Annotations to First Annotation View Example (VBA)
'********************************
'
' Preconditions: Open
document with at least two
' annotation
views. Each view should have at
' least
one annotation.
'
' Postconditions: Annotation and annotation view names
' printed
to debug window. All annotations moved
' to
the first annotation view.
'
'********************************
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelExt As SldWorks.ModelDocExtension
Dim swAnnViews As Variant 'Array
of annotation views
Dim vAnnotations As Variant 'Array
of annotations
Dim vAnnToMove() As SldWorks.Annotation 'Collections of
annotations to move
Dim swAnnView As SldWorks.AnnotationView
Dim i As Integer, j As Integer, k As Integer
Dim swAnn As SldWorks.Annotation
Dim boolstatus As Boolean
Sub main()
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Set swModelExt = swModel.Extension
ReDim vAnnToMove(0)
swAnnViews = swModelExt.AnnotationViews
'Activate each view; only one view can be active at a
time
For i = 0 To swModelExt.AnnotationViewCount
- 1
Set
swAnnView = swAnnViews(i)
Debug.Print
"Annotation view name: " & swAnnView.Name
swAnnView.Activate
'Alternative
to activate
'swAnnView.ActivateAndReorient
Next
'Print each annotation name and add all annotations to
a collection
For i = 0 To swModelExt.AnnotationViewCount
- 1
Set
swAnnView = swAnnViews(i)
swAnnView.Activate
vAnnotations
= swAnnView.Annotations
If
Not IsEmpty(vAnnotations) Then
For
j = 0 To UBound(vAnnotations)
Set
swAnn = vAnnotations(j)
Set
vAnnToMove(UBound(vAnnToMove)) = swAnn
ReDim
Preserve vAnnToMove(UBound(vAnnToMove))
Debug.Print
"Annotation name: " & swAnn.GetName
Next
End
If
'Move
all annotations to first annotation view
If
i > 0 Then
Set
swAnnView = swAnnViews(i)
boolstatus
= swAnnView.MoveAnnotations(vAnnToMove)
Debug.Assert
boolstatus
End
If
ReDim
vAnnToMove(0)
Next
End Sub